Search in sources :

Example 1 with OperationBatch

use of com.alibaba.graphscope.groot.operation.OperationBatch in project GraphScope by alibaba.

the class GraphWriter method writeBatch.

public void writeBatch(String requestId, String writeSession, List<WriteRequest> writeRequests, CompletionCallback<Long> callback) {
    this.pendingWriteCount.incrementAndGet();
    GraphSchema schema = snapshotCache.getSnapshotWithSchema().getGraphDef();
    OperationBatch.Builder batchBuilder = OperationBatch.newBuilder();
    for (WriteRequest writeRequest : writeRequests) {
        OperationType operationType = writeRequest.getOperationType();
        DataRecord dataRecord = writeRequest.getDataRecord();
        switch(operationType) {
            case OVERWRITE_VERTEX:
                addOverwriteVertexOperation(batchBuilder, schema, dataRecord);
                break;
            case UPDATE_VERTEX:
                addUpdateVertexOperation(batchBuilder, schema, dataRecord);
                break;
            case DELETE_VERTEX:
                addDeleteVertexOperation(batchBuilder, schema, dataRecord);
                break;
            case OVERWRITE_EDGE:
                addOverwriteEdgeOperation(batchBuilder, schema, dataRecord);
                break;
            case UPDATE_EDGE:
                addUpdateEdgeOperation(batchBuilder, schema, dataRecord);
                break;
            case DELETE_EDGE:
                addDeleteEdgeOperation(batchBuilder, schema, dataRecord);
                break;
            default:
                throw new IllegalArgumentException("Invalid operationType [" + operationType + "]");
        }
    }
    OperationBatch operationBatch = batchBuilder.build();
    int writeQueueId = getWriteQueueId(writeSession);
    int ingestorId = this.metaService.getIngestorIdForQueue(writeQueueId);
    long startTimeNano = System.nanoTime();
    this.ingestWriteClients.getClient(ingestorId).writeIngestorAsync(requestId, writeQueueId, operationBatch, new CompletionCallback<Long>() {

        @Override
        public void onCompleted(Long res) {
            long writeSnapshotId = res;
            lastWrittenSnapshotId.updateAndGet(x -> x < writeSnapshotId ? writeSnapshotId : x);
            writeRequestsTotal.addAndGet(writeRequests.size());
            finish();
            callback.onCompleted(res);
        }

        @Override
        public void onError(Throwable t) {
            finish();
            callback.onError(t);
        }

        void finish() {
            long ingestorCompleteTimeNano = System.nanoTime();
            ingestorBlockTimeNano.addAndGet(ingestorCompleteTimeNano - startTimeNano);
            pendingWriteCount.decrementAndGet();
        }
    });
}
Also used : VertexRecordKey(com.alibaba.maxgraph.sdkcommon.common.VertexRecordKey) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) PkHashUtils(com.alibaba.maxgraph.sdkcommon.util.PkHashUtils) EdgeRecordKey(com.alibaba.maxgraph.sdkcommon.common.EdgeRecordKey) OperationBatch(com.alibaba.graphscope.groot.operation.OperationBatch) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DataType(com.alibaba.maxgraph.compiler.api.schema.DataType) ArrayList(java.util.ArrayList) WriteSessionUtil(com.alibaba.maxgraph.common.util.WriteSessionUtil) PropertyDefNotFoundException(com.alibaba.maxgraph.compiler.api.exception.PropertyDefNotFoundException) MetricsCollector(com.alibaba.graphscope.groot.metrics.MetricsCollector) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetricsAgent(com.alibaba.graphscope.groot.metrics.MetricsAgent) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) VertexId(com.alibaba.graphscope.groot.operation.VertexId) com.alibaba.graphscope.groot.operation.dml(com.alibaba.graphscope.groot.operation.dml) Map(java.util.Map) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) MaxGraphException(com.alibaba.maxgraph.compiler.api.exception.MaxGraphException) EdgeId(com.alibaba.graphscope.groot.operation.EdgeId) CompletionCallback(com.alibaba.graphscope.groot.CompletionCallback) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) MetaService(com.alibaba.graphscope.groot.meta.MetaService) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) OperationType(com.alibaba.graphscope.groot.operation.OperationType) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) RoleClients(com.alibaba.graphscope.groot.rpc.RoleClients) List(java.util.List) SnapshotCache(com.alibaba.graphscope.groot.SnapshotCache) IngestorWriteClient(com.alibaba.graphscope.groot.frontend.IngestorWriteClient) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) OperationBatch(com.alibaba.graphscope.groot.operation.OperationBatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) OperationType(com.alibaba.graphscope.groot.operation.OperationType)

Example 2 with OperationBatch

use of com.alibaba.graphscope.groot.operation.OperationBatch in project GraphScope by alibaba.

the class BatchSender method asyncSendWithRetry.

public void asyncSendWithRetry(String requestId, int queueId, long snapshotId, long offset, OperationBatch operationBatch) {
    int partitionCount = metaService.getPartitionCount();
    Map<Integer, Builder> storeToBatchBuilder = new HashMap<>();
    Function<Integer, Builder> storeDataBatchBuilderFunc = k -> StoreDataBatch.newBuilder().requestId(requestId).queueId(queueId).snapshotId(snapshotId).offset(offset);
    for (OperationBlob operationBlob : operationBatch) {
        long partitionKey = operationBlob.getPartitionKey();
        if (partitionKey == -1L) {
            // replicate to all store node
            for (int i = 0; i < this.storeCount; i++) {
                StoreDataBatch.Builder batchBuilder = storeToBatchBuilder.computeIfAbsent(i, storeDataBatchBuilderFunc);
                batchBuilder.addOperation(-1, operationBlob);
            }
        } else {
            int partitionId = PartitionUtils.getPartitionIdFromKey(partitionKey, partitionCount);
            int storeId = metaService.getStoreIdByPartition(partitionId);
            StoreDataBatch.Builder batchBuilder = storeToBatchBuilder.computeIfAbsent(storeId, storeDataBatchBuilderFunc);
            batchBuilder.addOperation(partitionId, operationBlob);
        }
    }
    storeToBatchBuilder.forEach((storeId, batchBuilder) -> {
        while (!shouldStop) {
            try {
                storeSendBuffer.get(storeId).put(batchBuilder.build());
                break;
            } catch (InterruptedException e) {
                logger.warn("send buffer interrupted", e);
            }
        }
    });
}
Also used : Configs(com.alibaba.maxgraph.common.config.Configs) CommonConfig(com.alibaba.maxgraph.common.config.CommonConfig) LoggerFactory(org.slf4j.LoggerFactory) IngestorConfig(com.alibaba.maxgraph.common.config.IngestorConfig) OperationBatch(com.alibaba.graphscope.groot.operation.OperationBatch) StoreDataBatch(com.alibaba.graphscope.groot.operation.StoreDataBatch) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) MetricsCollector(com.alibaba.graphscope.groot.metrics.MetricsCollector) MetricsAgent(com.alibaba.graphscope.groot.metrics.MetricsAgent) Map(java.util.Map) Logger(org.slf4j.Logger) CompletionCallback(com.alibaba.graphscope.groot.CompletionCallback) BlockingQueue(java.util.concurrent.BlockingQueue) MetaService(com.alibaba.graphscope.groot.meta.MetaService) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) PartitionUtils(com.alibaba.maxgraph.sdkcommon.util.PartitionUtils) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) StoreConfig(com.alibaba.maxgraph.common.config.StoreConfig) List(java.util.List) AvgMetric(com.alibaba.graphscope.groot.metrics.AvgMetric) Builder(com.alibaba.graphscope.groot.operation.StoreDataBatch.Builder) OperationBlob(com.alibaba.graphscope.groot.operation.OperationBlob) Builder(com.alibaba.graphscope.groot.operation.StoreDataBatch.Builder) HashMap(java.util.HashMap) OperationBlob(com.alibaba.graphscope.groot.operation.OperationBlob) Builder(com.alibaba.graphscope.groot.operation.StoreDataBatch.Builder) StoreDataBatch(com.alibaba.graphscope.groot.operation.StoreDataBatch)

Example 3 with OperationBatch

use of com.alibaba.graphscope.groot.operation.OperationBatch in project GraphScope by alibaba.

the class SchemaManager method submitBatchDdl.

public void submitBatchDdl(String requestId, String sessionId, DdlRequestBatch ddlRequestBatch, CompletionCallback<Long> callback) {
    logger.info("submitBatchDdl requestId [" + requestId + "], sessionId [" + sessionId + "]");
    if (!ready) {
        callback.onError(new IllegalStateException("SchemaManager is recovering"));
        return;
    }
    this.singleThreadExecutor.execute(() -> {
        try {
            if (!ready) {
                callback.onError(new IllegalStateException("SchemaManager is recovering"));
                return;
            }
            GraphDef tmpGraphDef = this.graphDefRef.get();
            DdlResult ddlResult = this.ddlExecutors.executeDdlRequestBatch(ddlRequestBatch, tmpGraphDef, this.partitionCount);
            GraphDef graphDefResult = ddlResult.getGraphDef();
            List<Operation> ddlOperations = ddlResult.getDdlOperations();
            this.snapshotManager.lockWriteSnapshot();
            BatchId batchId;
            try {
                long currentWriteSnapshotId = this.snapshotManager.getCurrentWriteSnapshotId();
                OperationBatch operationBatch = OperationBatch.newBuilder(ddlOperations).setLatestSnapshotId(currentWriteSnapshotId).build();
                batchId = this.ddlWriter.writeOperations(requestId, operationBatch);
            } finally {
                this.snapshotManager.unlockWriteSnapshot();
            }
            long snapshotId = batchId.getSnapshotId();
            CompletableFuture<Void> future = new CompletableFuture<>();
            this.snapshotManager.addSnapshotListener(snapshotId, () -> {
                this.graphDefRef.set(graphDefResult);
                future.complete(null);
            });
            future.get();
            callback.onCompleted(snapshotId);
        } catch (Exception e) {
            logger.error("Error in Ddl requestId [" + requestId + "], sessionId [" + sessionId + "]", e);
            this.ready = false;
            callback.onError(e);
            this.singleThreadExecutor.execute(() -> recover());
        }
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) BatchId(com.alibaba.graphscope.groot.operation.BatchId) Operation(com.alibaba.graphscope.groot.operation.Operation) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) ServiceNotReadyException(com.alibaba.maxgraph.compiler.api.exception.ServiceNotReadyException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DdlResult(com.alibaba.graphscope.groot.schema.ddl.DdlResult) OperationBatch(com.alibaba.graphscope.groot.operation.OperationBatch)

Example 4 with OperationBatch

use of com.alibaba.graphscope.groot.operation.OperationBatch in project GraphScope by alibaba.

the class StoreService method writeStore.

private Map<Integer, OperationBatch> writeStore(long snapshotId, Map<Integer, OperationBatch> partitionToBatch, AtomicBoolean hasDdl) throws ExecutionException, InterruptedException {
    Map<Integer, OperationBatch> batchNeedRetry = new ConcurrentHashMap<>();
    AtomicInteger counter = new AtomicInteger(partitionToBatch.size());
    CompletableFuture<Object> future = new CompletableFuture<>();
    for (Map.Entry<Integer, OperationBatch> e : partitionToBatch.entrySet()) {
        int partitionId = e.getKey();
        OperationBatch batch = e.getValue();
        logger.debug("writeStore partition [" + partitionId + "]");
        this.writeExecutor.execute(() -> {
            try {
                if (partitionId != -1) {
                    // Ignore Marker
                    // Only support partition operation for now
                    long beforeWriteTime = System.nanoTime();
                    GraphPartition partition = this.idToPartition.get(partitionId);
                    if (partition == null) {
                        throw new IllegalStateException("partition [" + partitionId + "] is not initialized / exists");
                    }
                    if (partition.writeBatch(snapshotId, batch)) {
                        hasDdl.set(true);
                    }
                    long afterWriteTime = System.nanoTime();
                    this.partitionToMetric.get(partitionId).add(afterWriteTime - beforeWriteTime);
                }
            } catch (Exception ex) {
                logger.error("write to partition [" + partitionId + "] failed, snapshotId [" + snapshotId + "]. will retry", ex);
                batchNeedRetry.put(partitionId, batch);
            }
            if (counter.decrementAndGet() == 0) {
                future.complete(null);
            }
        });
    }
    future.get();
    if (batchNeedRetry.size() > 0) {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        // Ignore
        }
    }
    return batchNeedRetry;
}
Also used : MaxGraphException(com.alibaba.maxgraph.compiler.api.exception.MaxGraphException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OperationBatch(com.alibaba.graphscope.groot.operation.OperationBatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with OperationBatch

use of com.alibaba.graphscope.groot.operation.OperationBatch in project GraphScope by alibaba.

the class StoreService method batchWrite.

public boolean batchWrite(StoreDataBatch storeDataBatch) throws ExecutionException, InterruptedException {
    long snapshotId = storeDataBatch.getSnapshotId();
    List<Map<Integer, OperationBatch>> dataBatch = storeDataBatch.getDataBatch();
    AtomicBoolean hasDdl = new AtomicBoolean(false);
    for (Map<Integer, OperationBatch> partitionToBatch : dataBatch) {
        while (!shouldStop && partitionToBatch.size() != 0) {
            partitionToBatch = writeStore(snapshotId, partitionToBatch, hasDdl);
        }
    }
    return hasDdl.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OperationBatch(com.alibaba.graphscope.groot.operation.OperationBatch)

Aggregations

OperationBatch (com.alibaba.graphscope.groot.operation.OperationBatch)11 Map (java.util.Map)5 MetricsCollector (com.alibaba.graphscope.groot.metrics.MetricsCollector)4 Configs (com.alibaba.maxgraph.common.config.Configs)4 HashMap (java.util.HashMap)4 CompletionCallback (com.alibaba.graphscope.groot.CompletionCallback)3 MetaService (com.alibaba.graphscope.groot.meta.MetaService)3 LogReader (com.alibaba.graphscope.groot.wal.LogReader)3 ReadLogEntry (com.alibaba.graphscope.groot.wal.ReadLogEntry)3 IOException (java.io.IOException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.jupiter.api.Test)3 BatchSender (com.alibaba.graphscope.groot.ingestor.BatchSender)2 MetricsAgent (com.alibaba.graphscope.groot.metrics.MetricsAgent)2 OperationBlob (com.alibaba.graphscope.groot.operation.OperationBlob)2 StoreDataBatch (com.alibaba.graphscope.groot.operation.StoreDataBatch)2 VertexId (com.alibaba.graphscope.groot.operation.VertexId)2 LogEntry (com.alibaba.graphscope.groot.wal.LogEntry)2