2020
2121import org .apache .iotdb .commons .schema .column .ColumnHeaderConstant ;
2222import org .apache .iotdb .it .env .EnvFactory ;
23+ import org .apache .iotdb .it .env .cluster .env .SimpleEnv ;
2324import org .apache .iotdb .it .env .cluster .node .DataNodeWrapper ;
2425import org .apache .iotdb .it .framework .IoTDBTestRunner ;
2526import org .apache .iotdb .itbase .category .ClusterIT ;
4142import org .junit .After ;
4243import org .junit .Assert ;
4344import org .junit .Before ;
45+ import org .junit .Ignore ;
4446import org .junit .Test ;
4547import org .junit .experimental .categories .Category ;
4648import org .junit .runner .RunWith ;
4749
4850import java .io .IOException ;
4951import java .nio .charset .Charset ;
5052import java .nio .charset .StandardCharsets ;
53+ import java .sql .Connection ;
54+ import java .sql .ResultSet ;
55+ import java .sql .ResultSetMetaData ;
56+ import java .sql .SQLException ;
57+ import java .sql .Statement ;
5158import java .util .ArrayList ;
5259import java .util .Base64 ;
5360import java .util .List ;
5461import java .util .Map ;
62+ import java .util .concurrent .TimeUnit ;
5563
5664import static org .apache .iotdb .commons .schema .column .ColumnHeaderConstant .COLUMN_TTL ;
65+ import static org .apache .iotdb .consensus .ConsensusFactory .IOT_CONSENSUS ;
66+ import static org .apache .iotdb .consensus .ConsensusFactory .RATIS_CONSENSUS ;
5767import static org .junit .Assert .assertEquals ;
5868import static org .junit .Assert .assertTrue ;
5969import static org .junit .Assert .fail ;
@@ -79,7 +89,7 @@ public void tearDown() throws Exception {
7989 EnvFactory .getEnv ().cleanClusterEnvironment ();
8090 }
8191
82- private String getAuthorization (String username , String password ) {
92+ public static String getAuthorization (String username , String password ) {
8393 return Base64 .getEncoder ()
8494 .encodeToString ((username + ":" + password ).getBytes (StandardCharsets .UTF_8 ));
8595 }
@@ -129,7 +139,7 @@ public void ping() {
129139 }
130140 }
131141
132- private HttpPost getHttpPost (String url ) {
142+ public static HttpPost getHttpPost (String url ) {
133143 HttpPost httpPost = new HttpPost (url );
134144 httpPost .addHeader ("Content-type" , "application/json; charset=utf-8" );
135145 httpPost .setHeader ("Accept" , "application/json" );
@@ -243,6 +253,101 @@ public void errorInsertRecords(CloseableHttpClient httpClient, String json, Http
243253 }
244254 }
245255
256+ @ Ignore // Flaky test
257+ @ Test
258+ public void errorInsertRecords () throws SQLException , InterruptedException {
259+ SimpleEnv simpleEnv = new SimpleEnv ();
260+ simpleEnv
261+ .getConfig ()
262+ .getCommonConfig ()
263+ .setSchemaRegionConsensusProtocolClass (RATIS_CONSENSUS )
264+ .setSchemaReplicationFactor (3 )
265+ .setDataRegionConsensusProtocolClass (IOT_CONSENSUS )
266+ .setDataReplicationFactor (2 );
267+ simpleEnv .getConfig ().getDataNodeConfig ().setEnableRestService (true );
268+ simpleEnv .initClusterEnvironment (1 , 3 );
269+
270+ CloseableHttpResponse response = null ;
271+ CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
272+ try {
273+ HttpPost httpPost =
274+ getHttpPost (
275+ "http://"
276+ + simpleEnv .getDataNodeWrapper (0 ).getIp ()
277+ + ":"
278+ + simpleEnv .getDataNodeWrapper (0 ).getRestServicePort ()
279+ + "/rest/v2/insertRecords" );
280+ String json =
281+ "{\" timestamps\" :[1635232113960,1635232151960,1635232143960,1635232143960],\" measurements_list\" :[[\" s33\" ,\" s44\" ],[\" s55\" ,\" s66\" ],[\" s77\" ,\" s88\" ],[\" s771\" ,\" s881\" ]],\" data_types_list\" :[[\" INT32\" ,\" INT64\" ],[\" FLOAT\" ,\" DOUBLE\" ],[\" FLOAT\" ,\" DOUBLE\" ],[\" BOOLEAN\" ,\" TEXT\" ]],\" values_list\" :[[1,false],[2.1,2],[4,6],[false,\" cccccc\" ]],\" is_aligned\" :false,\" devices\" :[\" root.s1\" ,\" root.s1\" ,\" root.s1\" ,\" root.s3\" ]}" ;
282+ httpPost .setEntity (new StringEntity (json , Charset .defaultCharset ()));
283+ for (int i = 0 ; i < 30 ; i ++) {
284+ try {
285+ response = httpClient .execute (httpPost );
286+ break ;
287+ } catch (Exception e ) {
288+ if (i == 29 ) {
289+ throw e ;
290+ }
291+ try {
292+ Thread .sleep (1000 );
293+ } catch (InterruptedException ex ) {
294+ throw new RuntimeException (ex );
295+ }
296+ }
297+ }
298+
299+ HttpEntity responseEntity = response .getEntity ();
300+ String message = EntityUtils .toString (responseEntity , "utf-8" );
301+ JsonObject result = JsonParser .parseString (message ).getAsJsonObject ();
302+ assertEquals (507 , Integer .parseInt (result .get ("code" ).toString ()));
303+ } catch (IOException e ) {
304+ e .printStackTrace ();
305+ fail (e .getMessage ());
306+ } finally {
307+ try {
308+ if (response != null ) {
309+ response .close ();
310+ }
311+ } catch (IOException e ) {
312+ e .printStackTrace ();
313+ fail (e .getMessage ());
314+ }
315+ }
316+ TimeUnit .SECONDS .sleep (5 );
317+
318+ try {
319+ for (DataNodeWrapper dataNodeWrapper : simpleEnv .getDataNodeWrapperList ()) {
320+ dataNodeWrapper .stop ();
321+ try (Connection connectionAfterNodeDown = simpleEnv .getAvailableConnection ();
322+ Statement statementAfterNodeDown = connectionAfterNodeDown .createStatement ()) {
323+ int count = 0 ;
324+ try (ResultSet resultSet =
325+ statementAfterNodeDown .executeQuery (
326+ "select s88, s77, s66, s55, s44, s33 from root.s1" )) {
327+ ResultSetMetaData metaData = resultSet .getMetaData ();
328+ while (resultSet .next ()) {
329+ StringBuilder row = new StringBuilder ();
330+ for (int i = 0 ; i < metaData .getColumnCount (); i ++) {
331+ row .append (resultSet .getString (i + 1 )).append ("," );
332+ }
333+ System .out .println (row );
334+ count ++;
335+ }
336+ }
337+ assertEquals (3 , count );
338+ }
339+ dataNodeWrapper .start ();
340+ TimeUnit .SECONDS .sleep (1 );
341+ }
342+ } catch (SQLException e ) {
343+ if (!e .getMessage ().contains ("Maybe server is down" )) {
344+ throw e ;
345+ }
346+ } finally {
347+ simpleEnv .cleanClusterEnvironment ();
348+ }
349+ }
350+
246351 public void rightInsertTablet (CloseableHttpClient httpClient , String json , HttpPost httpPost ) {
247352 CloseableHttpResponse response = null ;
248353 try {
0 commit comments