use of com.alibaba.nacos.config.server.service.sql.ModifyRequest in project nacos by alibaba.
the class StandaloneDatabaseOperateImpl method dataImport.
@Override
public CompletableFuture<RestResult<String>> dataImport(File file) {
return CompletableFuture.supplyAsync(() -> {
try (DiskUtils.LineIterator iterator = DiskUtils.lineIterator(file)) {
int batchSize = 1000;
List<String> batchUpdate = new ArrayList<>(batchSize);
List<CompletableFuture<Void>> futures = new ArrayList<>();
List<Boolean> results = new CopyOnWriteArrayList<>();
while (iterator.hasNext()) {
String sql = iterator.next();
if (StringUtils.isNotBlank(sql)) {
batchUpdate.add(sql);
}
if (batchUpdate.size() == batchSize || !iterator.hasNext()) {
List<ModifyRequest> sqls = batchUpdate.stream().map(s -> {
ModifyRequest request = new ModifyRequest();
request.setSql(s);
return request;
}).collect(Collectors.toList());
futures.add(CompletableFuture.runAsync(() -> results.add(doDataImport(jdbcTemplate, sqls))));
batchUpdate.clear();
}
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
int code = 500;
if (!CollectionUtils.isEmpty(results)) {
code = (!results.stream().anyMatch(Boolean.FALSE::equals)) ? 200 : 500;
}
return RestResult.<String>builder().withCode(code).withData("").build();
} catch (Throwable ex) {
LogUtil.DEFAULT_LOG.error("An exception occurred when external data was imported into Derby : ", ex);
return RestResultUtils.failed(ex.getMessage());
}
});
}
use of com.alibaba.nacos.config.server.service.sql.ModifyRequest in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method update.
@Override
public Boolean update(List<ModifyRequest> sqlContext, BiConsumer<Boolean, Throwable> consumer) {
try {
// Since the SQL parameter is Object[], in order to ensure that the types of
// array elements are not lost, the serialization here is done using the java-specific
// serialization framework, rather than continuing with the protobuff
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "modifyRequests info : {}", sqlContext);
// {timestamp}-{group}-{ip:port}-{signature}
final String key = System.currentTimeMillis() + "-" + group() + "-" + memberManager.getSelf().getAddress() + "-" + MD5Utils.md5Hex(sqlContext.toString(), Constants.ENCODE);
WriteRequest request = WriteRequest.newBuilder().setGroup(group()).setKey(key).setData(ByteString.copyFrom(serializer.serialize(sqlContext))).putAllExtendInfo(EmbeddedStorageContextUtils.getCurrentExtendInfo()).setType(sqlContext.getClass().getCanonicalName()).build();
if (Objects.isNull(consumer)) {
Response response = this.protocol.write(request);
if (response.getSuccess()) {
return true;
}
LogUtil.DEFAULT_LOG.error("execute sql modify operation failed : {}", response.getErrMsg());
return false;
} else {
this.protocol.writeAsync(request).whenComplete((BiConsumer<Response, Throwable>) (response, ex) -> {
String errMsg = Objects.isNull(ex) ? response.getErrMsg() : ExceptionUtil.getCause(ex).getMessage();
consumer.accept(response.getSuccess(), StringUtils.isBlank(errMsg) ? null : new NJdbcException(errMsg));
});
}
return true;
} catch (TimeoutException e) {
LogUtil.FATAL_LOG.error("An timeout exception occurred during the update operation");
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
} catch (Throwable e) {
LogUtil.FATAL_LOG.error("An exception occurred during the update operation : {}", e);
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
}
}
use of com.alibaba.nacos.config.server.service.sql.ModifyRequest in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method dataImport.
@Override
public CompletableFuture<RestResult<String>> dataImport(File file) {
return CompletableFuture.supplyAsync(() -> {
try (DiskUtils.LineIterator iterator = DiskUtils.lineIterator(file)) {
int batchSize = 1000;
List<String> batchUpdate = new ArrayList<>(batchSize);
List<CompletableFuture<Response>> futures = new ArrayList<>();
while (iterator.hasNext()) {
String sql = iterator.next();
if (StringUtils.isNotBlank(sql)) {
batchUpdate.add(sql);
}
boolean submit = batchUpdate.size() == batchSize || !iterator.hasNext();
if (submit) {
List<ModifyRequest> requests = batchUpdate.stream().map(ModifyRequest::new).collect(Collectors.toList());
CompletableFuture<Response> future = protocol.writeAsync(WriteRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(serializer.serialize(requests))).putExtendInfo(DATA_IMPORT_KEY, Boolean.TRUE.toString()).build());
futures.add(future);
batchUpdate.clear();
}
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
for (CompletableFuture<Response> future : futures) {
Response response = future.get();
if (!response.getSuccess()) {
return RestResultUtils.failed(response.getErrMsg());
}
}
return RestResultUtils.success();
} catch (Throwable ex) {
LogUtil.DEFAULT_LOG.error("data import has error :", ex);
return RestResultUtils.failed(ex.getMessage());
}
});
}
use of com.alibaba.nacos.config.server.service.sql.ModifyRequest in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method onApply.
@Override
public Response onApply(WriteRequest log) {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "onApply info : log : {}", log);
final ByteString byteString = log.getData();
Preconditions.checkArgument(byteString != null, "Log.getData() must not null");
List<ModifyRequest> sqlContext = serializer.deserialize(byteString.toByteArray(), List.class);
final Lock lock = readLock;
lock.lock();
try {
boolean isOk = false;
if (log.containsExtendInfo(DATA_IMPORT_KEY)) {
isOk = doDataImport(jdbcTemplate, sqlContext);
} else {
sqlContext.sort(Comparator.comparingInt(ModifyRequest::getExecuteNo));
isOk = update(transactionTemplate, jdbcTemplate, sqlContext);
// If there is additional information, post processing
// Put into the asynchronous thread pool for processing to avoid blocking the
// normal execution of the state machine
ConfigExecutor.executeEmbeddedDump(() -> handleExtendInfo(log.getExtendInfoMap()));
}
return Response.newBuilder().setSuccess(isOk).build();
// We do not believe that an error caused by a problem with an SQL error
// should trigger the stop operation of the raft state machine
} catch (BadSqlGrammarException | DataIntegrityViolationException e) {
return Response.newBuilder().setSuccess(false).setErrMsg(e.toString()).build();
} catch (DataAccessException e) {
throw new ConsistencyException(e.toString());
} finally {
lock.unlock();
}
}
use of com.alibaba.nacos.config.server.service.sql.ModifyRequest in project nacos by alibaba.
the class EmbeddedStorageContextUtils_CITCase method test_multi_thread_sql_contexts.
@Test
public void test_multi_thread_sql_contexts() throws Exception {
CountDownLatch latch = new CountDownLatch(3);
ExecutorService service = Executors.newFixedThreadPool(3);
for (int i = 1; i < 4; i++) {
final int j = i;
service.submit(() -> {
try {
EmbeddedStorageContextUtils.addSqlContext("test_" + j, j);
EmbeddedStorageContextUtils.addSqlContext("test_" + j * 10, j);
List<ModifyRequest> list = EmbeddedStorageContextUtils.getCurrentSqlContext();
System.out.println(list);
Assert.assertEquals("test_" + j, list.get(0).getSql());
Assert.assertEquals("test_" + j * 10, list.get(0).getSql());
} finally {
latch.countDown();
}
});
}
latch.await();
}
Aggregations