Search in sources :

Example 6 with Operation

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

the class DdlExecutorTest method testExecutor.

@Test
void testExecutor() throws InvalidProtocolBufferException {
    PropertyValue defaultValue = new PropertyValue(DataType.INT, ByteBuffer.allocate(Integer.BYTES).putInt(1).array());
    PropertyDef propertyDef = new PropertyDef(1, 1, "p1", DataType.INT, defaultValue, true, "property_1");
    TypeDef vertexTypeDef = TypeDef.newBuilder().setLabel("vertex1").addPropertyDef(propertyDef).setTypeEnum(TypeEnum.VERTEX).build();
    TypeDef edgeTypeDef = TypeDef.newBuilder().setLabel("edge1").addPropertyDef(propertyDef).setTypeEnum(TypeEnum.EDGE).build();
    EdgeKind edgeKind = EdgeKind.newBuilder().setEdgeLabel("edge1").setDstVertexLabel("vertex1").setSrcVertexLabel("vertex1").build();
    GraphDef graphDef = GraphDef.newBuilder().build();
    DdlExecutors ddlExecutors = new DdlExecutors();
    DdlRequestBatch.Builder requestBatchBuilder = DdlRequestBatch.newBuilder();
    requestBatchBuilder.addDdlRequest(new CreateVertexTypeRequest(vertexTypeDef));
    requestBatchBuilder.addDdlRequest(new CreateEdgeTypeRequest(edgeTypeDef));
    requestBatchBuilder.addDdlRequest(new AddEdgeKindRequest(edgeKind));
    DdlRequestBatch ddlRequestBatch = requestBatchBuilder.build();
    // Test serde
    ByteString bytes = ddlRequestBatch.toProto().toByteString();
    assertEquals(DdlRequestBatch.parseProto(DdlRequestBatchPb.parseFrom(bytes)), ddlRequestBatch);
    DdlResult ddlResult = ddlExecutors.executeDdlRequestBatch(ddlRequestBatch, graphDef, 1);
    LabelId vertexLabelId = new LabelId(1);
    LabelId edgeLabelId = new LabelId(2);
    EdgeKind edgeKindWithId = EdgeKind.newBuilder(edgeKind).setEdgeLabelId(edgeLabelId).setSrcVertexLabelId(vertexLabelId).setDstVertexLabelId(vertexLabelId).build();
    TypeDef vertexTypeWithId = TypeDef.newBuilder(vertexTypeDef).setLabelId(vertexLabelId).build();
    TypeDef edgeTypeWithId = TypeDef.newBuilder(edgeTypeDef).setLabelId(edgeLabelId).build();
    graphDef = GraphDef.newBuilder(graphDef).setVersion(3).setLabelIdx(2).setPropertyIdx(1).putPropertyNameToId("p1", 1).addTypeDef(vertexTypeWithId).putVertexTableId(vertexTypeWithId.getTypeLabelId(), 1L).addTypeDef(edgeTypeWithId).addEdgeKind(edgeKindWithId).putEdgeTableId(edgeKindWithId, 2L).setTableIdx(2L).build();
    assertEquals(graphDef, ddlResult.getGraphDef());
    List<Operation> ddlOperations = ddlResult.getDdlOperations();
    assertEquals(ddlOperations.size(), 3);
    assertEquals(ddlOperations.get(0).toBlob().toProto(), new CreateVertexTypeOperation(0, 1L, vertexTypeWithId, 1L).toBlob().toProto());
    assertEquals(ddlOperations.get(1).toBlob().toProto(), new CreateEdgeTypeOperation(0, 2L, edgeTypeWithId).toBlob().toProto());
    assertEquals(ddlOperations.get(2).toBlob().toProto(), new AddEdgeKindOperation(0, 3L, edgeKindWithId, 2L).toBlob().toProto());
    assertEquals(GraphDef.parseProto(GraphDefPb.parseFrom(graphDef.toProto().toByteString())), graphDef);
    requestBatchBuilder = DdlRequestBatch.newBuilder();
    requestBatchBuilder.addDdlRequest(new RemoveEdgeKindRequest(edgeKind));
    requestBatchBuilder.addDdlRequest(new DropEdgeTypeRequest(edgeTypeDef.getLabel()));
    requestBatchBuilder.addDdlRequest(new DropVertexTypeRequest(vertexTypeDef.getLabel()));
    ddlResult = ddlExecutors.executeDdlRequestBatch(requestBatchBuilder.build(), graphDef, 1);
    graphDef = GraphDef.newBuilder(graphDef).setVersion(6).removeEdgeKind(edgeKindWithId).removeTypeDef(edgeTypeDef.getLabel()).removeTypeDef(vertexTypeDef.getLabel()).clearUnusedPropertyName(Collections.emptySet()).build();
    assertEquals(graphDef, ddlResult.getGraphDef());
    ddlOperations = ddlResult.getDdlOperations();
    assertEquals(ddlOperations.size(), 3);
    assertEquals(ddlOperations.get(0).toBlob().toProto(), new RemoveEdgeKindOperation(0, 4L, edgeKindWithId).toBlob().toProto());
    assertEquals(ddlOperations.get(1).toBlob().toProto(), new DropEdgeTypeOperation(0, 5L, edgeLabelId).toBlob().toProto());
    assertEquals(ddlOperations.get(2).toBlob().toProto(), new DropVertexTypeOperation(0, 6L, vertexLabelId).toBlob().toProto());
}
Also used : PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) ByteString(com.google.protobuf.ByteString) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) CreateVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateVertexTypeOperation) CreateEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateEdgeTypeOperation) DropVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropVertexTypeOperation) Operation(com.alibaba.graphscope.groot.operation.Operation) DropEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropEdgeTypeOperation) RemoveEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation) AddEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation) RemoveEdgeKindRequest(com.alibaba.graphscope.groot.schema.request.RemoveEdgeKindRequest) DropVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropVertexTypeOperation) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) CreateVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateVertexTypeOperation) AddEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation) DropVertexTypeRequest(com.alibaba.graphscope.groot.schema.request.DropVertexTypeRequest) DdlRequestBatch(com.alibaba.graphscope.groot.schema.request.DdlRequestBatch) CreateEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateEdgeTypeOperation) RemoveEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation) AddEdgeKindRequest(com.alibaba.graphscope.groot.schema.request.AddEdgeKindRequest) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) DdlExecutors(com.alibaba.graphscope.groot.schema.ddl.DdlExecutors) DropEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropEdgeTypeOperation) CreateVertexTypeRequest(com.alibaba.graphscope.groot.schema.request.CreateVertexTypeRequest) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) DropEdgeTypeRequest(com.alibaba.graphscope.groot.schema.request.DropEdgeTypeRequest) CreateEdgeTypeRequest(com.alibaba.graphscope.groot.schema.request.CreateEdgeTypeRequest) DdlResult(com.alibaba.graphscope.groot.schema.ddl.DdlResult) Test(org.junit.jupiter.api.Test)

Example 7 with Operation

use of com.alibaba.graphscope.groot.operation.Operation 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 8 with Operation

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

the class CommitDataLoadExecutor method execute.

@Override
public DdlResult execute(ByteString ddlBlob, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    CommitDataLoadPb commitDataLoadPb = CommitDataLoadPb.parseFrom(ddlBlob);
    DataLoadTargetPb dataLoadTargetPb = commitDataLoadPb.getTarget();
    DataLoadTarget dataLoadTarget = DataLoadTarget.parseProto(dataLoadTargetPb);
    String label = dataLoadTarget.getLabel();
    String srcLabel = dataLoadTarget.getSrcLabel();
    String dstLabel = dataLoadTarget.getDstLabel();
    long version = graphDef.getSchemaVersion();
    if (!graphDef.hasLabel(label)) {
        throw new DdlException("label [" + label + "] not exists, schema version [" + version + "]");
    }
    GraphDef.Builder graphDefBuilder = GraphDef.newBuilder(graphDef);
    TypeDef typeDef = graphDef.getTypeDef(label);
    DataLoadTarget.Builder targetBuilder = DataLoadTarget.newBuilder(dataLoadTarget);
    if (srcLabel == null || srcLabel.isEmpty()) {
        // Vertex type
        if (typeDef.getTypeEnum() != TypeEnum.VERTEX) {
            throw new DdlException("invalid data load target [" + dataLoadTarget + "], label is not a vertex");
        }
        targetBuilder.setLabelId(typeDef.getLabelId());
    } else {
        // Edge kind
        if (typeDef.getTypeEnum() != TypeEnum.EDGE) {
            throw new DdlException("invalid data load target [" + dataLoadTarget + "], label is not an edge");
        }
        EdgeKind.Builder edgeKindBuilder = EdgeKind.newBuilder();
        LabelId edgeLabelId = graphDef.getLabelId(label);
        if (edgeLabelId == null) {
            throw new DdlException("invalid edgeLabel [" + label + "], schema version [" + version + "]");
        }
        edgeKindBuilder.setEdgeLabelId(edgeLabelId);
        targetBuilder.setLabelId(edgeLabelId.getId());
        LabelId srcVertexLabelId = graphDef.getLabelId(srcLabel);
        if (srcVertexLabelId == null) {
            throw new DdlException("invalid srcVertexLabel [" + srcLabel + "], schema version [" + version + "]");
        }
        edgeKindBuilder.setSrcVertexLabelId(srcVertexLabelId);
        targetBuilder.setSrcLabelId(srcVertexLabelId.getId());
        LabelId dstVertexLabelId = graphDef.getLabelId(dstLabel);
        if (dstVertexLabelId == null) {
            throw new DdlException("invalid dstVertexLabel [" + dstLabel + "], schema version [" + version + "]");
        }
        edgeKindBuilder.setDstVertexLabelId(dstVertexLabelId);
        targetBuilder.setDstLabelId(dstVertexLabelId.getId());
        EdgeKind edgeKind = edgeKindBuilder.build();
        if (!graphDef.hasEdgeKind(edgeKind)) {
            throw new DdlException("invalid data load target [" + dataLoadTarget + "], edgeKind not exists");
        }
    }
    version++;
    graphDefBuilder.setVersion(version);
    GraphDef newGraphDef = graphDefBuilder.build();
    List<Operation> operations = new ArrayList<>(partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        operations.add(new CommitDataLoadOperation(i, version, CommitDataLoadPb.newBuilder().setTableIdx(commitDataLoadPb.getTableIdx()).setTarget(targetBuilder.build().toProto()).build()));
    }
    return new DdlResult(newGraphDef, operations);
}
Also used : DdlException(com.alibaba.graphscope.groot.schema.request.DdlException) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) CommitDataLoadOperation(com.alibaba.graphscope.groot.operation.ddl.CommitDataLoadOperation) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Operation(com.alibaba.graphscope.groot.operation.Operation) CommitDataLoadOperation(com.alibaba.graphscope.groot.operation.ddl.CommitDataLoadOperation) CommitDataLoadPb(com.alibaba.maxgraph.proto.CommitDataLoadPb) DataLoadTargetPb(com.alibaba.maxgraph.proto.DataLoadTargetPb) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) DataLoadTarget(com.alibaba.maxgraph.sdkcommon.common.DataLoadTarget) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId)

Example 9 with Operation

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

the class DdlExecutors method executeDdlRequestBatch.

public DdlResult executeDdlRequestBatch(DdlRequestBatch ddlRequestBatch, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    List<Operation> operations = new ArrayList<>();
    GraphDef tmpGraphDef = graphDef;
    for (DdlRequestBlob ddlRequestBlob : ddlRequestBatch) {
        OperationType operationType = ddlRequestBlob.getOperationType();
        ByteString ddlBlob = ddlRequestBlob.getBytes();
        DdlResult ddlResult = getExecutor(operationType).execute(ddlBlob, tmpGraphDef, partitionCount);
        operations.addAll(ddlResult.getDdlOperations());
        tmpGraphDef = ddlResult.getGraphDef();
    }
    return new DdlResult(tmpGraphDef, operations);
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) DdlRequestBlob(com.alibaba.graphscope.groot.schema.request.DdlRequestBlob) Operation(com.alibaba.graphscope.groot.operation.Operation) OperationType(com.alibaba.graphscope.groot.operation.OperationType) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef)

Aggregations

Operation (com.alibaba.graphscope.groot.operation.Operation)9 GraphDef (com.alibaba.maxgraph.sdkcommon.schema.GraphDef)9 ByteString (com.google.protobuf.ByteString)8 LabelId (com.alibaba.maxgraph.sdkcommon.schema.LabelId)7 ArrayList (java.util.ArrayList)7 DdlException (com.alibaba.graphscope.groot.schema.request.DdlException)6 EdgeKind (com.alibaba.maxgraph.sdkcommon.schema.EdgeKind)6 TypeDef (com.alibaba.maxgraph.sdkcommon.schema.TypeDef)5 PropertyDef (com.alibaba.maxgraph.sdkcommon.schema.PropertyDef)3 AddEdgeKindOperation (com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation)2 RemoveEdgeKindOperation (com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation)2 DdlResult (com.alibaba.graphscope.groot.schema.ddl.DdlResult)2 DataLoadTargetPb (com.alibaba.maxgraph.proto.DataLoadTargetPb)2 EdgeKindPb (com.alibaba.maxgraph.proto.groot.EdgeKindPb)2 TypeDefPb (com.alibaba.maxgraph.proto.groot.TypeDefPb)2 DataLoadTarget (com.alibaba.maxgraph.sdkcommon.common.DataLoadTarget)2 BatchId (com.alibaba.graphscope.groot.operation.BatchId)1 OperationBatch (com.alibaba.graphscope.groot.operation.OperationBatch)1 OperationType (com.alibaba.graphscope.groot.operation.OperationType)1 CommitDataLoadOperation (com.alibaba.graphscope.groot.operation.ddl.CommitDataLoadOperation)1