|
74 | 74 | import org.apache.pulsar.common.policies.data.FunctionStatsImpl;
|
75 | 75 | import org.apache.pulsar.common.util.Codec;
|
76 | 76 | import org.apache.pulsar.common.util.RestException;
|
| 77 | +import org.apache.pulsar.functions.api.state.StateValue; |
77 | 78 | import org.apache.pulsar.functions.instance.InstanceUtils;
|
78 | 79 | import org.apache.pulsar.functions.instance.state.DefaultStateStore;
|
79 | 80 | import org.apache.pulsar.functions.proto.Function;
|
@@ -1151,23 +1152,29 @@ public FunctionState getFunctionState(final String tenant,
|
1151 | 1152 |
|
1152 | 1153 | try {
|
1153 | 1154 | DefaultStateStore store = worker().getStateStoreProvider().getStateStore(tenant, namespace, functionName);
|
1154 |
| - ByteBuffer buf = store.get(key); |
1155 |
| - if (buf == null) { |
| 1155 | + StateValue value = store.getStateValue(key); |
| 1156 | + if (value == null) { |
| 1157 | + throw new RestException(Status.NOT_FOUND, "key '" + key + "' doesn't exist."); |
| 1158 | + } |
| 1159 | + byte[] data = value.getValue(); |
| 1160 | + if (data == null) { |
1156 | 1161 | throw new RestException(Status.NOT_FOUND, "key '" + key + "' doesn't exist.");
|
1157 | 1162 | }
|
1158 | 1163 |
|
1159 |
| - // try to parse the state as a long |
1160 |
| - // but even if it can be parsed as a long, this number may not be the actual state, |
1161 |
| - // so we will always return a `stringValue` or `bytesValue` with the number value |
| 1164 | + ByteBuffer buf = ByteBuffer.wrap(data); |
| 1165 | + |
1162 | 1166 | Long number = null;
|
1163 | 1167 | if (buf.remaining() == Long.BYTES) {
|
1164 | 1168 | number = buf.getLong();
|
1165 | 1169 | }
|
| 1170 | + if (Boolean.TRUE.equals(value.getIsNumber())) { |
| 1171 | + return new FunctionState(key, null, null, number, value.getVersion()); |
| 1172 | + } |
1166 | 1173 |
|
1167 |
| - if (Utf8.isWellFormed(buf.array())) { |
1168 |
| - return new FunctionState(key, new String(buf.array(), UTF_8), null, number, null); |
| 1174 | + if (Utf8.isWellFormed(data)) { |
| 1175 | + return new FunctionState(key, new String(data, UTF_8), null, number, value.getVersion()); |
1169 | 1176 | } else {
|
1170 |
| - return new FunctionState(key, null, buf.array(), number, null); |
| 1177 | + return new FunctionState(key, null, data, number, value.getVersion()); |
1171 | 1178 | }
|
1172 | 1179 | } catch (RestException e) {
|
1173 | 1180 | throw e;
|
@@ -1215,7 +1222,7 @@ public void putFunctionState(final String tenant,
|
1215 | 1222 | try {
|
1216 | 1223 | DefaultStateStore store = worker().getStateStoreProvider().getStateStore(tenant, namespace, functionName);
|
1217 | 1224 | ByteBuffer data;
|
1218 |
| - if (StringUtils.isNotEmpty(state.getStringValue())) { |
| 1225 | + if (state.getStringValue() != null) { |
1219 | 1226 | data = ByteBuffer.wrap(state.getStringValue().getBytes(UTF_8));
|
1220 | 1227 | } else if (state.getByteValue() != null) {
|
1221 | 1228 | data = ByteBuffer.wrap(state.getByteValue());
|
|
0 commit comments