Skip to content

Commit

Permalink
refactor confignode idevice
Browse files Browse the repository at this point in the history
Signed-off-by: OneSizeFitQuorum <[email protected]>
  • Loading branch information
OneSizeFitsQuorum committed Jun 13, 2024
1 parent 3f8b2b1 commit 7fb5d16
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import org.apache.iotdb.itbase.category.ClusterIT;
import org.apache.iotdb.rpc.TSStatusCode;

import org.apache.tsfile.file.metadata.IDeviceID;
import org.apache.tsfile.file.metadata.IDeviceID.Factory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand All @@ -61,8 +63,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -346,12 +348,12 @@ public void testGetSlots() throws Exception {
final String sg0 = "root.sg0";
final String sg1 = "root.sg1";

final String d00 = sg0 + ".d0.s";
final String d01 = sg0 + ".d1.s";
final String d10 = sg1 + ".d0.s";
final String d11 = sg1 + ".d1.s";
final IDeviceID d00 = Factory.DEFAULT_FACTORY.create(sg0 + ".d0.s");
final IDeviceID d01 = Factory.DEFAULT_FACTORY.create(sg0 + ".d1.s");
final IDeviceID d10 = Factory.DEFAULT_FACTORY.create(sg1 + ".d0.s");
final IDeviceID d11 = Factory.DEFAULT_FACTORY.create(sg1 + ".d1.s");

String[] devices = new String[] {d00, d01, d10, d11};
IDeviceID[] devices = new IDeviceID[] {d00, d01, d10, d11};

try (SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) {
Expand Down Expand Up @@ -431,8 +433,10 @@ public void testGetSlots() throws Exception {
// Get RegionId with device

TGetRegionIdReq deviceReq = new TGetRegionIdReq(TConsensusGroupType.DataRegion);
for (String device : devices) {
deviceReq.setDevice(device.getBytes(StandardCharsets.UTF_8));
for (IDeviceID device : devices) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
device.serialize(baos);
deviceReq.setDevice(baos.toByteArray());
TGetRegionIdResp resp = client.getRegionId(deviceReq);
Assert.assertEquals(
TSStatusCode.SUCCESS_STATUS.getStatusCode(), resp.getStatus().getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -611,20 +610,10 @@ public TDatabaseSchema getDatabaseSchemaByName(String database)
*
* @return The DatabaseName of the specified Device. Empty String if not exists.
*/
public String getDatabaseNameByDevice(String devicePath) {
List<String> databases = getDatabaseNames();
for (String database : databases) {
if (PathUtils.isStartWith(devicePath, database)) {
return database;
}
}
return "";
}

public String getDatabaseNameByDevice(byte[] devicePath) {
List<String> databases = getDatabaseNames();
for (String database : databases) {
if (PathUtils.isStartWith(Arrays.toString(devicePath), database)) {
if (PathUtils.isStartWith(devicePath, database)) {
return database;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2329,7 +2329,9 @@ public SettableFuture<ConfigTaskResult> getRegionId(GetRegionIdStatement getRegi
final TGetRegionIdReq tGetRegionIdReq =
new TGetRegionIdReq(getRegionIdStatement.getPartitionType());
if (getRegionIdStatement.getDevice() != null) {
tGetRegionIdReq.setDevice(getRegionIdStatement.getDevice());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
getRegionIdStatement.getDevice().serialize(baos);
tGetRegionIdReq.setDevice(baos.toByteArray());
} else {
tGetRegionIdReq.setDatabase(getRegionIdStatement.getDatabase());
}
Expand Down Expand Up @@ -2383,7 +2385,9 @@ public SettableFuture<ConfigTaskResult> getTimeSlotList(
if (getTimeSlotListStatement.getDatabase() != null) {
tGetTimeSlotListReq.setDatabase(getTimeSlotListStatement.getDatabase());
} else if (getTimeSlotListStatement.getDevice() != null) {
tGetTimeSlotListReq.setDevice(getTimeSlotListStatement.getDevice());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
getTimeSlotListStatement.getDevice().serialize(baos);
tGetTimeSlotListReq.setDevice(baos.toByteArray());
} else if (getTimeSlotListStatement.getRegionId() != -1) {
tGetTimeSlotListReq.setRegionId(getTimeSlotListStatement.getRegionId());
}
Expand Down Expand Up @@ -2416,7 +2420,9 @@ public SettableFuture<ConfigTaskResult> countTimeSlotList(
if (countTimeSlotListStatement.getDatabase() != null) {
tCountTimeSlotListReq.setDatabase(countTimeSlotListStatement.getDatabase());
} else if (countTimeSlotListStatement.getDevice() != null) {
tCountTimeSlotListReq.setDevice(countTimeSlotListStatement.getDevice());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
countTimeSlotListStatement.getDevice().serialize(baos);
tCountTimeSlotListReq.setDevice(baos.toByteArray());
} else if (countTimeSlotListStatement.getRegionId() != -1) {
tCountTimeSlotListReq.setRegionId(countTimeSlotListStatement.getRegionId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
import org.apache.tsfile.common.conf.TSFileDescriptor;
import org.apache.tsfile.common.constant.TsFileConstant;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.file.metadata.IDeviceID.Factory;
import org.apache.tsfile.file.metadata.enums.CompressionType;
import org.apache.tsfile.file.metadata.enums.TSEncoding;
import org.apache.tsfile.read.common.TimeRange;
Expand All @@ -226,7 +227,6 @@
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -3883,7 +3883,7 @@ public Statement visitGetRegionId(IoTDBSqlParser.GetRegionIdContext ctx) {
if (ctx.database != null) {
getRegionIdStatement.setDatabase(ctx.database.getText());
} else {
getRegionIdStatement.setDevice(ctx.device.getText().getBytes(StandardCharsets.UTF_8));
getRegionIdStatement.setDevice(Factory.DEFAULT_FACTORY.create(ctx.device.getText()));
}
getRegionIdStatement.setStartTimeStamp(-1L);
getRegionIdStatement.setEndTimeStamp(Long.MAX_VALUE);
Expand Down Expand Up @@ -3952,7 +3952,7 @@ public Statement visitGetTimeSlotList(IoTDBSqlParser.GetTimeSlotListContext ctx)
if (ctx.database != null) {
getTimeSlotListStatement.setDatabase(ctx.database.getText());
} else if (ctx.device != null) {
getTimeSlotListStatement.setDevice(ctx.device.getText().getBytes(StandardCharsets.UTF_8));
getTimeSlotListStatement.setDevice(Factory.DEFAULT_FACTORY.create(ctx.device.getText()));
} else if (ctx.regionId != null) {
getTimeSlotListStatement.setRegionId(Integer.parseInt(ctx.regionId.getText()));
}
Expand All @@ -3973,7 +3973,7 @@ public Statement visitCountTimeSlotList(IoTDBSqlParser.CountTimeSlotListContext
if (ctx.database != null) {
countTimeSlotListStatement.setDatabase(ctx.database.getText());
} else if (ctx.device != null) {
countTimeSlotListStatement.setDevice(ctx.device.getText().getBytes(StandardCharsets.UTF_8));
countTimeSlotListStatement.setDevice(Factory.DEFAULT_FACTORY.create(ctx.device.getText()));
} else if (ctx.regionId != null) {
countTimeSlotListStatement.setRegionId(Integer.parseInt(ctx.regionId.getText()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.iotdb.db.queryengine.plan.statement.Statement;
import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor;

import org.apache.tsfile.file.metadata.IDeviceID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,7 +38,7 @@ public class CountTimeSlotListStatement extends Statement implements IConfigStat

private String database;

private byte[] device;
private IDeviceID device;

private long regionId = -1;

Expand Down Expand Up @@ -75,11 +76,11 @@ public void setEndTime(long endTime) {
this.endTime = endTime;
}

public void setDevice(byte[] device) {
public void setDevice(IDeviceID device) {
this.device = device;
}

public byte[] getDevice() {
public IDeviceID getDevice() {
return this.device;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.iotdb.db.queryengine.plan.statement.Statement;
import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor;

import org.apache.tsfile.file.metadata.IDeviceID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,7 +48,7 @@ public class GetRegionIdStatement extends Statement implements IConfigStatement

private String database;

private byte[] device;
private IDeviceID device;
private final TConsensusGroupType partitionType;
private long startTimeStamp;

Expand Down Expand Up @@ -84,15 +85,15 @@ public TConsensusGroupType getPartitionType() {
return partitionType;
}

public byte[] getDevice() {
public IDeviceID getDevice() {
return device;
}

public void setDatabase(String database) {
this.database = database;
}

public void setDevice(byte[] device) {
public void setDevice(IDeviceID device) {
this.device = device;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.iotdb.db.queryengine.plan.statement.Statement;
import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor;

import org.apache.tsfile.file.metadata.IDeviceID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -46,7 +47,7 @@ public class GetTimeSlotListStatement extends Statement implements IConfigStatem

private String database;

private byte[] device;
private IDeviceID device;

private long regionId = -1;

Expand Down Expand Up @@ -84,11 +85,11 @@ public void setEndTime(long endTime) {
this.endTime = endTime;
}

public void setDevice(byte[] device) {
public void setDevice(IDeviceID device) {
this.device = device;
}

public byte[] getDevice() {
public IDeviceID getDevice() {
return this.device;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import org.apache.tsfile.common.constant.TsFileConstant;
import org.apache.tsfile.exception.PathParseException;
import org.apache.tsfile.file.metadata.IDeviceID;
import org.apache.tsfile.file.metadata.IDeviceID.Deserializer;
import org.apache.tsfile.read.common.parser.PathNodesGenerator;
import org.apache.tsfile.read.common.parser.PathVisitor;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -192,6 +194,13 @@ public static boolean isStartWith(IDeviceID deviceID, String storageGroup) {
return deviceName.equals(storageGroup) || deviceName.startsWith(storageGroup + ".");
}

public static boolean isStartWith(byte[] deviceID, String storageGroup) {
// TODO: use correct judgement without converting to string
String deviceName =
Deserializer.DEFAULT_DESERIALIZER.deserializeFrom(ByteBuffer.wrap(deviceID)).toString();
return deviceName.equals(storageGroup) || deviceName.startsWith(storageGroup + ".");
}

/** Remove the back quotes of a measurement if necessary */
public static String removeBackQuotesIfNecessary(String measurement) {
String unWrapped = measurement.substring(1, measurement.length() - 1);
Expand Down

0 comments on commit 7fb5d16

Please sign in to comment.