Skip to content

Commit

Permalink
Activate schema template in batches. (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
liyuheng55555 authored Dec 20, 2023
1 parent 5279632 commit 2ddef35
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/** this class will create more than one connection. */
public class IoTDB implements IDatabase {

private static final Logger LOGGER = LoggerFactory.getLogger(IoTDB.class);
private static final String ALREADY_KEYWORD = "already";
private static final AtomicBoolean templateInit = new AtomicBoolean(false);
private static final int ACTIVATE_TEMPLATE_THRESHOLD = 1000;
protected final String DELETE_SERIES_SQL;
protected SingleNodeJDBCConnection ioTDBConnection;

Expand Down Expand Up @@ -303,15 +303,28 @@ private void registerStorageGroups(Session metaSession, List<TimeseriesSchema> s
}

private void activateTemplate(Session metaSession, List<TimeseriesSchema> schemaList) {
try {
List<String> devicePaths =
schemaList.stream()
.map(schema -> ROOT_SERIES_NAME + "." + schema.getDeviceSchema().getDevicePath())
.collect(Collectors.toList());
metaSession.createTimeseriesUsingSchemaTemplate(devicePaths);
} catch (Throwable t) {
t.printStackTrace();
}
List<String> partialDevicePaths = new ArrayList<>();
AtomicLong activatedDeviceCount = new AtomicLong();
schemaList.stream()
.map(schema -> ROOT_SERIES_NAME + "." + schema.getDeviceSchema().getDevicePath())
.forEach(
path -> {
partialDevicePaths.add(path);
if (partialDevicePaths.size() >= ACTIVATE_TEMPLATE_THRESHOLD) {
try {
metaSession.createTimeseriesUsingSchemaTemplate(partialDevicePaths);
} catch (Exception e) {
LOGGER.error(
"Activate {}~{} devices' schema template fail",
activatedDeviceCount.get(),
activatedDeviceCount.get() + partialDevicePaths.size(),
e);
System.exit(1);
}
activatedDeviceCount.addAndGet(partialDevicePaths.size());
partialDevicePaths.clear();
}
});
}

private TimeseriesSchema createTimeseries(DeviceSchema deviceSchema) {
Expand Down

0 comments on commit 2ddef35

Please sign in to comment.