This is an automated email from the ASF dual-hosted git repository.

CritasWang pushed a commit to branch feat/schema-export-tags-attributes
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to 
refs/heads/feat/schema-export-tags-attributes by this push:
     new d07f3f68240 [Tools] Exit non-zero when import-schema connection 
retries are exhausted
d07f3f68240 is described below

commit d07f3f682408a1d08fcd51aabe675564a8456bd5
Author: CritasWang <[email protected]>
AuthorDate: Fri Nov 27 16:35:10 2026 +0800

    [Tools] Exit non-zero when import-schema connection retries are exhausted
    
    The connection failure was already reported to the console and the
    failed rows were written to the failed-record file, but writeScheme
    threw IllegalStateException after all of that work was done and the
    calling importSchemaFromCsvFile only caught IOException /
    IllegalPathException, so the exception was swallowed by the generic
    run() handler and importFromTargetPath returned CODE_OK (0).
    
    Replace the raw IllegalStateException with a named inner class
    ConnectionRetriesExhaustedException. importSchemaFromCsvFile now
    catches that type specifically, sets connectionFailed, and prints the
    error message. importFromTargetPath checks the flag after all files
    are processed and returns CODE_ERROR when it is set.
    
    New tests in SchemaTagsImportTest:
    - exhaustedConnectionRetriesMarkTheImportAsFailed: runs
      importSchemaFromCsvFile against a pool that always throws
      IoTDBConnectionException for both aligned and non-aligned paths;
      asserts connectionFailed is true and the failed-record file contains
      the row.
    - successfulImportDoesNotMarkTheImportAsFailed: confirms the flag
      stays false when no failure occurs.
    
    Verified: import against an unreachable port exits 1 and prints the
    failure message for both aligned and non-aligned paths; all 37 cli
    schema unit tests pass on both en and zh-locale compile.
---
 .../iotdb/tool/schema/AbstractSchemaTool.java      |  6 +--
 .../org/apache/iotdb/tool/schema/ImportSchema.java |  8 +--
 .../apache/iotdb/tool/schema/ImportSchemaTree.java | 20 ++++++--
 .../iotdb/tool/schema/SchemaTagsImportTest.java    | 60 ++++++++++++++++++++++
 4 files changed, 79 insertions(+), 15 deletions(-)

diff --git 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractSchemaTool.java
 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractSchemaTool.java
index 5740cabd67f..ca98672ba4f 100644
--- 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractSchemaTool.java
+++ 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractSchemaTool.java
@@ -82,11 +82,7 @@ public abstract class AbstractSchemaTool {
   protected static String targetFile = Constants.DUMP_FILE_NAME_DEFAULT;
   protected static final LongAdder loadFileSuccessfulNum = new LongAdder();
 
-  /**
-   * Set to {@code true} when a connection-retry failure is surfaced from an 
import file; checked by
-   * the entry point to produce a non-zero exit status even though the 
per-file reporting has
-   * already printed the failure and written the failed-record file.
-   */
+  /** Set when an imported file ran out of connection retries, so the import 
exits non-zero. */
   protected static volatile boolean connectionFailed = false;
 
   private static final IoTPrinter ioTPrinter = new IoTPrinter(System.out);
diff --git 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchema.java 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchema.java
index c565e5dcfcb..8cd03ea42bf 100644
--- 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchema.java
+++ 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchema.java
@@ -161,12 +161,8 @@ public class ImportSchema extends AbstractSchemaTool {
       }
       importSchema.init();
       AbstractImportSchema.init(importSchema);
-      if (connectionFailed) {
-        ioTPrinter.println(
-            String.format(CliMessages.MESSAGE_IMPORT_SCHEMA_FAIL_ARG_609A6D30, 
"connection"));
-        return Constants.CODE_ERROR;
-      }
-      return Constants.CODE_OK;
+      // the failure itself has already been printed by the file that hit it
+      return connectionFailed ? Constants.CODE_ERROR : Constants.CODE_OK;
     } catch (InterruptedException e) {
       ioTPrinter.println(
           String.format(CliMessages.MESSAGE_IMPORT_SCHEMA_FAIL_ARG_609A6D30, 
e.getMessage()));
diff --git 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchemaTree.java
 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchemaTree.java
index 8d51c052026..97221091539 100644
--- 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchemaTree.java
+++ 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ImportSchemaTree.java
@@ -125,10 +125,13 @@ public class ImportSchemaTree extends 
AbstractImportSchema {
       }
       writeScheme(file.getName(), headerNames, records, failedFilePath);
       processSuccessFile();
-    } catch (IllegalStateException e) {
-      // A connection-retry failure is already reported and the failed rows 
are already written;
-      // set the flag so the entry point can exit with a non-zero status code.
+    } catch (ConnectionRetriesExhaustedException e) {
+      // The failed rows are already in the failed-record file; keep importing 
the remaining files
+      // and let the entry point turn the flag into a non-zero exit status.
       connectionFailed = true;
+      ioTPrinter.println(
+          String.format(
+              CliMessages.MESSAGE_IMPORT_SCHEMA_FAIL_ARG_609A6D30, 
String.valueOf(e.getMessage())));
     } catch (IOException | IllegalPathException e) {
       ioTPrinter.println(
           String.format(
@@ -575,7 +578,7 @@ public class ImportSchemaTree extends AbstractImportSchema {
     if (connectionFailures.get() != null) {
       // The failed rows are already in the failed-record file; surface the 
error so that the
       // command exits with a non-zero status instead of reporting success.
-      throw new IllegalStateException(connectionFailures.get());
+      throw new ConnectionRetriesExhaustedException(connectionFailures.get());
     }
   }
 
@@ -781,6 +784,15 @@ public class ImportSchemaTree extends AbstractImportSchema 
{
     return null;
   }
 
+  /**
+   * Raised by {@link #writeScheme} after the rows hit by exhausted connection 
retries are saved.
+   */
+  static class ConnectionRetriesExhaustedException extends 
IllegalStateException {
+    ConnectionRetriesExhaustedException(String message) {
+      super(message);
+    }
+  }
+
   private static void writeFailedLinesFile(
       String failedFilePath, ArrayList<List<Object>> failedRecords) {
     int fileIndex = 0;
diff --git 
a/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/schema/SchemaTagsImportTest.java
 
b/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/schema/SchemaTagsImportTest.java
index ed6c0a7411a..aabe90c62b8 100644
--- 
a/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/schema/SchemaTagsImportTest.java
+++ 
b/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/schema/SchemaTagsImportTest.java
@@ -73,6 +73,8 @@ public class SchemaTagsImportTest {
     ImportSchemaTree.sessionPool = null;
     ImportSchemaTree.aligned = false;
     ImportSchemaTree.batchPointSize = Constants.BATCH_POINT_SIZE;
+    ImportSchemaTree.failedFileDirectory = null;
+    AbstractSchemaTool.connectionFailed = false;
   }
 
   @Test
@@ -609,6 +611,64 @@ public class SchemaTagsImportTest {
     assertTrue(failed, failed.contains("root.sg.d.s1"));
   }
 
+  /**
+   * The exhausted retry must survive the per-file handler: {@code run()} 
swallows every exception,
+   * so the flag is the only way the entry point learns that it has to exit 
non-zero.
+   */
+  @Test
+  public void exhaustedConnectionRetriesMarkTheImportAsFailed() throws 
Exception {
+    for (boolean useAligned : new boolean[] {false, true}) {
+      AbstractSchemaTool.connectionFailed = false;
+      ImportSchemaTree.aligned = useAligned;
+      SessionPool pool = mock(SessionPool.class);
+      ImportSchemaTree.sessionPool = pool;
+      IoTDBConnectionException down =
+          new IoTDBConnectionException(new IllegalStateException("down"));
+      doThrow(down)
+          .when(pool)
+          .createMultiTimeseries(any(), any(), any(), any(), any(), any(), 
any(), any());
+      doThrow(down)
+          .when(pool)
+          .createAlignedTimeseries(any(), any(), any(), any(), any(), any(), 
any(), any());
+      doThrow(down).when(pool).createAlignedTimeseries(any(), any(), any(), 
any(), any(), any());
+
+      File directory = temporaryFolder.newFolder("exhausted_" + useAligned);
+      File csv = new File(directory, "schema.csv");
+      Files.write(
+          csv.toPath(),
+          (Constants.HEAD_COLUMNS_V2.stream().collect(Collectors.joining(","))
+                  + "\nroot.sg.d.s1,,INT64,TS_2DIFF,SNAPPY,,\n")
+              .getBytes(StandardCharsets.UTF_8));
+      ImportSchemaTree.failedFileDirectory = directory.getAbsolutePath() + 
File.separator;
+
+      new ImportSchemaTree().importSchemaFromCsvFile(csv);
+
+      assertTrue("aligned=" + useAligned, AbstractSchemaTool.connectionFailed);
+      File[] failedFiles = directory.listFiles((dir, name) -> 
name.startsWith("schema.csv.failed"));
+      assertTrue("aligned=" + useAligned, failedFiles != null && 
failedFiles.length == 1);
+      String failed =
+          new String(Files.readAllBytes(failedFiles[0].toPath()), 
StandardCharsets.UTF_8);
+      assertTrue(failed, failed.contains("root.sg.d.s1"));
+    }
+  }
+
+  @Test
+  public void successfulImportDoesNotMarkTheImportAsFailed() throws Exception {
+    ImportSchemaTree.sessionPool = mock(SessionPool.class);
+    File directory = temporaryFolder.newFolder("ok");
+    File csv = new File(directory, "schema.csv");
+    Files.write(
+        csv.toPath(),
+        (Constants.HEAD_COLUMNS_V2.stream().collect(Collectors.joining(","))
+                + "\nroot.sg.d.s1,,INT64,TS_2DIFF,SNAPPY,,\n")
+            .getBytes(StandardCharsets.UTF_8));
+    ImportSchemaTree.failedFileDirectory = directory.getAbsolutePath() + 
File.separator;
+
+    new ImportSchemaTree().importSchemaFromCsvFile(csv);
+
+    assertFalse(AbstractSchemaTool.connectionFailed);
+  }
+
   private String readFailedFile() throws Exception {
     File[] files = temporaryFolder.getRoot().listFiles((dir, name) -> 
name.startsWith("failed"));
     assertTrue("the failed-record file must be written", files != null && 
files.length > 0);

Reply via email to