Search in sources :

Example 1 with SnapshotListener

use of com.alibaba.graphscope.groot.SnapshotListener in project GraphScope by alibaba.

the class SnapshotCacheTest method testSnapshotCache.

@Test
void testSnapshotCache() {
    SnapshotCache snapshotCache = new SnapshotCache();
    GraphDef graphDef = null;
    snapshotCache.advanceQuerySnapshotId(5L, graphDef);
    assertEquals(snapshotCache.getSnapshotWithSchema().getSnapshotId(), 5L);
    SnapshotListener listener1 = mock(SnapshotListener.class);
    snapshotCache.addListener(5L, listener1);
    verify(listener1, times(1)).onSnapshotAvailable();
    SnapshotListener listener2 = mock(SnapshotListener.class);
    snapshotCache.addListener(6L, listener2);
    snapshotCache.advanceQuerySnapshotId(6L, graphDef);
    verify(listener2, times(1)).onSnapshotAvailable();
    snapshotCache.advanceQuerySnapshotId(7L, graphDef);
    verify(listener1, times(1)).onSnapshotAvailable();
    verify(listener2, times(1)).onSnapshotAvailable();
}
Also used : SnapshotCache(com.alibaba.graphscope.groot.SnapshotCache) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) SnapshotListener(com.alibaba.graphscope.groot.SnapshotListener) Test(org.junit.jupiter.api.Test)

Example 2 with SnapshotListener

use of com.alibaba.graphscope.groot.SnapshotListener in project GraphScope by alibaba.

the class SchemaManagerTest method testSchemaManager.

@Test
void testSchemaManager() throws IOException, InterruptedException {
    SnapshotManager mockSnapshotManager = mock(SnapshotManager.class);
    doAnswer(invocationOnMock -> {
        SnapshotListener listener = invocationOnMock.getArgument(1);
        listener.onSnapshotAvailable();
        return null;
    }).when(mockSnapshotManager).addSnapshotListener(anyLong(), any());
    when(mockSnapshotManager.increaseWriteSnapshotId()).thenReturn(1L);
    when(mockSnapshotManager.getCurrentWriteSnapshotId()).thenReturn(1L);
    DdlExecutors ddlExecutors = new DdlExecutors();
    DdlWriter mockDdlWriter = mock(DdlWriter.class);
    when(mockDdlWriter.writeOperations(anyString(), any())).thenReturn(new BatchId(1L));
    MetaService mockMetaService = mock(MetaService.class);
    GraphDefFetcher mockGraphDefFetcher = mock(GraphDefFetcher.class);
    GraphDef initialGraphDef = GraphDef.newBuilder().build();
    when(mockGraphDefFetcher.fetchGraphDef()).thenReturn(initialGraphDef);
    SchemaManager schemaManager = new SchemaManager(mockSnapshotManager, ddlExecutors, mockDdlWriter, mockMetaService, mockGraphDefFetcher);
    schemaManager.start();
    assertEquals(initialGraphDef, schemaManager.getGraphDef());
    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 typeDef = TypeDef.newBuilder().setLabel("vertex1").addPropertyDef(propertyDef).setTypeEnum(TypeEnum.VERTEX).build();
    DdlRequestBatch ddlRequestBatch = DdlRequestBatch.newBuilder().addDdlRequest(new CreateVertexTypeRequest(typeDef)).build();
    CountDownLatch latch = new CountDownLatch(1);
    schemaManager.submitBatchDdl("requestId", "sessionId", ddlRequestBatch, new CompletionCallback<Long>() {

        @Override
        public void onCompleted(Long res) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable t) {
        }
    });
    assertTrue(latch.await(5L, TimeUnit.SECONDS));
    schemaManager.stop();
}
Also used : MetaService(com.alibaba.graphscope.groot.meta.MetaService) PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) BatchId(com.alibaba.graphscope.groot.operation.BatchId) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) SchemaManager(com.alibaba.graphscope.groot.coordinator.SchemaManager) CountDownLatch(java.util.concurrent.CountDownLatch) GraphDefFetcher(com.alibaba.graphscope.groot.coordinator.GraphDefFetcher) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) SnapshotListener(com.alibaba.graphscope.groot.SnapshotListener) DdlWriter(com.alibaba.graphscope.groot.coordinator.DdlWriter) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) DdlExecutors(com.alibaba.graphscope.groot.schema.ddl.DdlExecutors) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) CreateVertexTypeRequest(com.alibaba.graphscope.groot.schema.request.CreateVertexTypeRequest) DdlRequestBatch(com.alibaba.graphscope.groot.schema.request.DdlRequestBatch) SnapshotManager(com.alibaba.graphscope.groot.coordinator.SnapshotManager) Test(org.junit.jupiter.api.Test)

Example 3 with SnapshotListener

use of com.alibaba.graphscope.groot.SnapshotListener in project GraphScope by alibaba.

the class SnapshotManager method maybeUpdateQuerySnapshotId.

private void maybeUpdateQuerySnapshotId() {
    if (this.storeToSnapshotInfo.size() < this.storeCount) {
        logger.warn("Not all store nodes reported snapshot progress. current storeToSnapshot [" + this.storeToSnapshotInfo + "]");
        return;
    }
    SnapshotInfo minSnapshotInfo = Collections.min(this.storeToSnapshotInfo.values());
    if (minSnapshotInfo.getSnapshotId() > this.querySnapshotInfo.getSnapshotId()) {
        synchronized (this.querySnapshotLock) {
            long snapshotId = minSnapshotInfo.getSnapshotId();
            long ddlSnapshotId = minSnapshotInfo.getDdlSnapshotId();
            long currentSnapshotId = this.querySnapshotInfo.getSnapshotId();
            long currentDdlSnapshotId = this.querySnapshotInfo.getDdlSnapshotId();
            if (snapshotId > currentSnapshotId) {
                try {
                    if (ddlSnapshotId < currentDdlSnapshotId) {
                        // During failover, store might send smaller ddlSnapshotId
                        minSnapshotInfo = new SnapshotInfo(snapshotId, currentDdlSnapshotId);
                    // throw new
                    // IllegalStateException("minSnapshotInfo [" + minSnapshotInfo +
                    // "], currentSnapshotInfo [" +
                    // this.querySnapshotInfo + "]");
                    }
                    persistQuerySnapshotId(minSnapshotInfo);
                    this.querySnapshotInfo = minSnapshotInfo;
                    logger.debug("querySnapshotInfo updated to [" + minSnapshotInfo + "]");
                } catch (IOException e) {
                    logger.error("update querySnapshotInfo failed", e);
                    return;
                }
                long newSnapshotId = minSnapshotInfo.getSnapshotId();
                long newDdlSnapshotId = minSnapshotInfo.getDdlSnapshotId();
                NavigableMap<Long, List<SnapshotListener>> listenersToTrigger = this.snapshotToListeners.headMap(newSnapshotId, true);
                for (Map.Entry<Long, List<SnapshotListener>> listenerEntry : listenersToTrigger.entrySet()) {
                    List<SnapshotListener> listeners = listenerEntry.getValue();
                    for (SnapshotListener listener : listeners) {
                        try {
                            listener.onSnapshotAvailable();
                        } catch (Exception e) {
                            logger.warn("trigger snapshotListener failed. snapshotId [" + snapshotId + "]");
                        }
                    }
                }
                listenersToTrigger.clear();
                for (QuerySnapshotListener listener : this.listeners) {
                    try {
                        listener.snapshotAdvanced(newSnapshotId, newDdlSnapshotId);
                    } catch (Exception e) {
                        logger.error("error occurred when notify normal listeners", e);
                    }
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IOException(java.io.IOException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) SnapshotListener(com.alibaba.graphscope.groot.SnapshotListener) MaxGraphException(com.alibaba.maxgraph.compiler.api.exception.MaxGraphException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

SnapshotListener (com.alibaba.graphscope.groot.SnapshotListener)3 GraphDef (com.alibaba.maxgraph.sdkcommon.schema.GraphDef)2 Test (org.junit.jupiter.api.Test)2 SnapshotCache (com.alibaba.graphscope.groot.SnapshotCache)1 DdlWriter (com.alibaba.graphscope.groot.coordinator.DdlWriter)1 GraphDefFetcher (com.alibaba.graphscope.groot.coordinator.GraphDefFetcher)1 SchemaManager (com.alibaba.graphscope.groot.coordinator.SchemaManager)1 SnapshotManager (com.alibaba.graphscope.groot.coordinator.SnapshotManager)1 MetaService (com.alibaba.graphscope.groot.meta.MetaService)1 BatchId (com.alibaba.graphscope.groot.operation.BatchId)1 DdlExecutors (com.alibaba.graphscope.groot.schema.ddl.DdlExecutors)1 CreateVertexTypeRequest (com.alibaba.graphscope.groot.schema.request.CreateVertexTypeRequest)1 DdlRequestBatch (com.alibaba.graphscope.groot.schema.request.DdlRequestBatch)1 MaxGraphException (com.alibaba.maxgraph.compiler.api.exception.MaxGraphException)1 PropertyDef (com.alibaba.maxgraph.sdkcommon.schema.PropertyDef)1 PropertyValue (com.alibaba.maxgraph.sdkcommon.schema.PropertyValue)1 TypeDef (com.alibaba.maxgraph.sdkcommon.schema.TypeDef)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1