Skip to content

Commit 1a035c4

Browse files
authored
Print measurement for putting buffer exceptions (#15873)
1 parent f2f0050 commit 1a035c4

5 files changed

Lines changed: 108 additions & 56 deletions

File tree

integration-test/src/test/java/org/apache/iotdb/session/it/IoTDBSessionSimpleIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import static org.junit.Assert.assertEquals;
8080
import static org.junit.Assert.assertFalse;
8181
import static org.junit.Assert.assertNull;
82+
import static org.junit.Assert.assertThrows;
8283
import static org.junit.Assert.assertTrue;
8384
import static org.junit.Assert.fail;
8485

@@ -2200,4 +2201,19 @@ public void testQueryAllDataType() throws IoTDBConnectionException, StatementExe
22002201
}
22012202
}
22022203
}
2204+
2205+
@Test
2206+
public void testInsertWrongTypeRecord() throws IoTDBConnectionException {
2207+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
2208+
assertThrows(
2209+
ClassCastException.class,
2210+
() ->
2211+
session.insertRecord(
2212+
"root.db1.d1",
2213+
0,
2214+
Collections.singletonList("s1"),
2215+
Collections.singletonList(TSDataType.INT32),
2216+
Collections.singletonList(1L)));
2217+
}
2218+
}
22032219
}

iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ private TSInsertRecordReq genTSInsertRecordReq(
15571557
request.setPrefixPath(prefixPath);
15581558
request.setTimestamp(time);
15591559
request.setMeasurements(measurements);
1560-
ByteBuffer buffer = SessionUtils.getValueBuffer(types, values);
1560+
ByteBuffer buffer = SessionUtils.getValueBuffer(types, values, measurements);
15611561
request.setValues(buffer);
15621562
request.setIsAligned(isAligned);
15631563
return request;
@@ -2498,7 +2498,8 @@ private TSInsertRecordsOfOneDeviceReq genTSInsertRecordsOfOneDeviceReq(
24982498
request.setPrefixPath(prefixPath);
24992499
request.setTimestamps(times);
25002500
request.setMeasurementsList(measurementsList);
2501-
List<ByteBuffer> buffersList = objectValuesListToByteBufferList(valuesList, typesList);
2501+
List<ByteBuffer> buffersList =
2502+
objectValuesListToByteBufferList(valuesList, typesList, measurementsList);
25022503
request.setValuesList(buffersList);
25032504
request.setIsAligned(isAligned);
25042505
return request;
@@ -2572,11 +2573,14 @@ private static <T> List<T> sortList(List<T> source, Integer[] index) {
25722573
}
25732574

25742575
private List<ByteBuffer> objectValuesListToByteBufferList(
2575-
List<List<Object>> valuesList, List<List<TSDataType>> typesList)
2576+
List<List<Object>> valuesList,
2577+
List<List<TSDataType>> typesList,
2578+
List<List<String>> measurementsList)
25762579
throws IoTDBConnectionException {
25772580
List<ByteBuffer> buffersList = new ArrayList<>();
25782581
for (int i = 0; i < valuesList.size(); i++) {
2579-
ByteBuffer buffer = SessionUtils.getValueBuffer(typesList.get(i), valuesList.get(i));
2582+
ByteBuffer buffer =
2583+
SessionUtils.getValueBuffer(typesList.get(i), valuesList.get(i), measurementsList.get(i));
25802584
buffersList.add(buffer);
25812585
}
25822586
return buffersList;
@@ -2652,7 +2656,8 @@ private TSInsertRecordsReq genTSInsertRecordsReq(
26522656
request.setTimestamps(times);
26532657
request.setMeasurementsList(measurementsList);
26542658
request.setIsAligned(isAligned);
2655-
List<ByteBuffer> buffersList = objectValuesListToByteBufferList(valuesList, typesList);
2659+
List<ByteBuffer> buffersList =
2660+
objectValuesListToByteBufferList(valuesList, typesList, measurementsList);
26562661
request.setValuesList(buffersList);
26572662
return request;
26582663
}
@@ -2689,7 +2694,7 @@ private void updateTSInsertRecordsReq(
26892694
request.addToPrefixPaths(deviceId);
26902695
request.addToTimestamps(time);
26912696
request.addToMeasurementsList(measurements);
2692-
ByteBuffer buffer = SessionUtils.getValueBuffer(types, values);
2697+
ByteBuffer buffer = SessionUtils.getValueBuffer(types, values, measurements);
26932698
request.addToValuesList(buffer);
26942699
}
26952700

iotdb-client/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.tsfile.write.UnSupportedDataTypeException;
3535
import org.apache.tsfile.write.record.Tablet;
3636
import org.apache.tsfile.write.schema.IMeasurementSchema;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
3739

3840
import java.nio.ByteBuffer;
3941
import java.time.LocalDate;
@@ -44,6 +46,7 @@
4446

4547
public class SessionUtils {
4648

49+
private static final Logger LOGGER = LoggerFactory.getLogger(SessionUtils.class);
4750
private static final byte TYPE_NULL = -2;
4851
private static final int EMPTY_DATE_INT = 10000101;
4952

@@ -145,10 +148,11 @@ private static int calOccupationOfOneColumn(
145148
return valueOccupation;
146149
}
147150

148-
public static ByteBuffer getValueBuffer(List<TSDataType> types, List<Object> values)
151+
public static ByteBuffer getValueBuffer(
152+
List<TSDataType> types, List<Object> values, List<String> measurements)
149153
throws IoTDBConnectionException {
150154
ByteBuffer buffer = ByteBuffer.allocate(SessionUtils.calculateLength(types, values));
151-
SessionUtils.putValues(types, values, buffer);
155+
SessionUtils.putValues(types, values, buffer, measurements);
152156
return buffer;
153157
}
154158

@@ -204,53 +208,60 @@ private static int calculateLength(List<TSDataType> types, List<Object> values)
204208
* @param buffer buffer to insert
205209
* @throws IoTDBConnectionException
206210
*/
207-
private static void putValues(List<TSDataType> types, List<Object> values, ByteBuffer buffer)
211+
private static void putValues(
212+
List<TSDataType> types, List<Object> values, ByteBuffer buffer, List<String> measurements)
208213
throws IoTDBConnectionException {
209214
for (int i = 0; i < values.size(); i++) {
210-
if (values.get(i) == null) {
211-
ReadWriteIOUtils.write(TYPE_NULL, buffer);
212-
continue;
213-
}
214-
ReadWriteIOUtils.write(types.get(i), buffer);
215-
switch (types.get(i)) {
216-
case BOOLEAN:
217-
ReadWriteIOUtils.write((Boolean) values.get(i), buffer);
218-
break;
219-
case INT32:
220-
ReadWriteIOUtils.write((Integer) values.get(i), buffer);
221-
break;
222-
case DATE:
223-
ReadWriteIOUtils.write(
224-
DateUtils.parseDateExpressionToInt((LocalDate) values.get(i)), buffer);
225-
break;
226-
case INT64:
227-
case TIMESTAMP:
228-
ReadWriteIOUtils.write((Long) values.get(i), buffer);
229-
break;
230-
case FLOAT:
231-
ReadWriteIOUtils.write((Float) values.get(i), buffer);
232-
break;
233-
case DOUBLE:
234-
ReadWriteIOUtils.write((Double) values.get(i), buffer);
235-
break;
236-
case TEXT:
237-
case STRING:
238-
byte[] bytes;
239-
if (values.get(i) instanceof Binary) {
215+
try {
216+
if (values.get(i) == null) {
217+
ReadWriteIOUtils.write(TYPE_NULL, buffer);
218+
continue;
219+
}
220+
ReadWriteIOUtils.write(types.get(i), buffer);
221+
switch (types.get(i)) {
222+
case BOOLEAN:
223+
ReadWriteIOUtils.write((Boolean) values.get(i), buffer);
224+
break;
225+
case INT32:
226+
ReadWriteIOUtils.write((Integer) values.get(i), buffer);
227+
break;
228+
case DATE:
229+
ReadWriteIOUtils.write(
230+
DateUtils.parseDateExpressionToInt((LocalDate) values.get(i)), buffer);
231+
break;
232+
case INT64:
233+
case TIMESTAMP:
234+
ReadWriteIOUtils.write((Long) values.get(i), buffer);
235+
break;
236+
case FLOAT:
237+
ReadWriteIOUtils.write((Float) values.get(i), buffer);
238+
break;
239+
case DOUBLE:
240+
ReadWriteIOUtils.write((Double) values.get(i), buffer);
241+
break;
242+
case TEXT:
243+
case STRING:
244+
byte[] bytes;
245+
if (values.get(i) instanceof Binary) {
246+
bytes = ((Binary) values.get(i)).getValues();
247+
} else {
248+
bytes = ((String) values.get(i)).getBytes(TSFileConfig.STRING_CHARSET);
249+
}
250+
ReadWriteIOUtils.write(bytes.length, buffer);
251+
buffer.put(bytes);
252+
break;
253+
case BLOB:
240254
bytes = ((Binary) values.get(i)).getValues();
241-
} else {
242-
bytes = ((String) values.get(i)).getBytes(TSFileConfig.STRING_CHARSET);
243-
}
244-
ReadWriteIOUtils.write(bytes.length, buffer);
245-
buffer.put(bytes);
246-
break;
247-
case BLOB:
248-
bytes = ((Binary) values.get(i)).getValues();
249-
ReadWriteIOUtils.write(bytes.length, buffer);
250-
buffer.put(bytes);
251-
break;
252-
default:
253-
throw new IoTDBConnectionException(MSG_UNSUPPORTED_DATA_TYPE + types.get(i));
255+
ReadWriteIOUtils.write(bytes.length, buffer);
256+
buffer.put(bytes);
257+
break;
258+
default:
259+
throw new IoTDBConnectionException(MSG_UNSUPPORTED_DATA_TYPE + types.get(i));
260+
}
261+
} catch (Throwable e) {
262+
LOGGER.error(
263+
"Cannot put values for measurement {}, type={}", measurements.get(i), types.get(i), e);
264+
throw e;
254265
}
255266
}
256267
buffer.flip();

iotdb-client/session/src/test/java/org/apache/iotdb/session/util/SessionUtilsTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.util.Collections;
4141
import java.util.List;
4242

43+
import static org.junit.Assert.assertThrows;
44+
4345
public class SessionUtilsTest {
4446

4547
@Test
@@ -125,19 +127,20 @@ public void testGetValueBuffer2() throws IoTDBConnectionException {
125127
TSDataType.DOUBLE,
126128
TSDataType.TEXT,
127129
TSDataType.BOOLEAN);
128-
ByteBuffer timeBuffer = SessionUtils.getValueBuffer(typeList, valueList);
130+
List<String> measurements = Arrays.asList("s1", "s2", "s3", "s4", "s5", "s6");
131+
ByteBuffer timeBuffer = SessionUtils.getValueBuffer(typeList, valueList, measurements);
129132
Assert.assertNotNull(timeBuffer);
130133

131134
valueList = new ArrayList<>();
132135
valueList.add(null);
133136
typeList = Collections.singletonList(TSDataType.INT32);
134-
timeBuffer = SessionUtils.getValueBuffer(typeList, valueList);
137+
timeBuffer = SessionUtils.getValueBuffer(typeList, valueList, measurements);
135138
Assert.assertNotNull(timeBuffer);
136139

137140
valueList = Collections.singletonList(false);
138141
typeList = Collections.singletonList(TSDataType.UNKNOWN);
139142
try {
140-
SessionUtils.getValueBuffer(typeList, valueList);
143+
SessionUtils.getValueBuffer(typeList, valueList, measurements);
141144
} catch (Exception e) {
142145
Assert.assertTrue(e instanceof IoTDBConnectionException);
143146
}
@@ -204,6 +207,23 @@ public void testGetValueBuffer3() {
204207
Assert.assertNotNull(timeBuffer);
205208
}
206209

210+
@Test
211+
public void testGetValueBufferWithWrongType() {
212+
List<Object> valueList = Arrays.asList(12L, 13, 1.2, 0.707f, false, "false");
213+
List<TSDataType> typeList =
214+
Arrays.asList(
215+
TSDataType.INT32,
216+
TSDataType.INT64,
217+
TSDataType.FLOAT,
218+
TSDataType.DOUBLE,
219+
TSDataType.TEXT,
220+
TSDataType.BOOLEAN);
221+
List<String> measurements = Arrays.asList("s1", "s2", "s3", "s4", "s5", "s6");
222+
assertThrows(
223+
ClassCastException.class,
224+
() -> SessionUtils.getValueBuffer(typeList, valueList, measurements));
225+
}
226+
207227
@Test
208228
public void testParseSeedNodeUrls() {
209229
List<String> nodeUrls = Collections.singletonList("127.0.0.1:1234");

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/IoTDBInternalLocalReporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private TSStatus insertRecord(Map<String, Object> valueMap, String prefix, long
191191
types.add(inferType(value));
192192
values.add(value);
193193
}
194-
ByteBuffer buffer = SessionUtils.getValueBuffer(types, values);
194+
ByteBuffer buffer = SessionUtils.getValueBuffer(types, values, measurements);
195195

196196
request.setPrefixPath(prefix);
197197
request.setTimestamp(time);

0 commit comments

Comments
 (0)