Search in sources :

Example 1 with StorageException

use of uk.gov.gchq.gaffer.federatedstore.exception.StorageException in project Gaffer by gchq.

the class FederatedGraphStorage method changeGraphId.

private boolean changeGraphId(final String graphId, final String newGraphId, final Predicate<FederatedAccess> accessPredicate) throws StorageException {
    boolean rtn;
    final Graph graphToMove = getGraphToMove(graphId, accessPredicate);
    if (nonNull(graphToMove)) {
        FederatedAccess key = null;
        // remove graph to be moved from storage
        for (final Entry<FederatedAccess, Set<Graph>> entry : storage.entrySet()) {
            final boolean removed = entry.getValue().removeIf(graph -> graph.getGraphId().equals(graphId));
            if (removed) {
                key = entry.getKey();
                break;
            }
        }
        // Update Tables
        String storeClass = graphToMove.getStoreProperties().getStoreClass();
        if (nonNull(storeClass) && storeClass.startsWith(AccumuloStore.class.getPackage().getName())) {
            /*
                 * This logic is only for Accumulo derived stores Only.
                 * For updating table names to match graphs names.
                 *
                 * uk.gov.gchq.gaffer.accumulostore.[AccumuloStore, SingleUseAccumuloStore,
                 * SingleUseMockAccumuloStore, MockAccumuloStore, MiniAccumuloStore]
                 */
            try {
                AccumuloProperties tmpAccumuloProps = (AccumuloProperties) graphToMove.getStoreProperties();
                Connector connection = TableUtils.getConnector(tmpAccumuloProps.getInstance(), tmpAccumuloProps.getZookeepers(), tmpAccumuloProps.getUser(), tmpAccumuloProps.getPassword());
                if (connection.tableOperations().exists(graphId)) {
                    connection.tableOperations().offline(graphId);
                    connection.tableOperations().rename(graphId, newGraphId);
                    connection.tableOperations().online(newGraphId);
                }
            } catch (final Exception e) {
                LOGGER.warn("Error trying to update tables for graphID:{} graphToMove:{}", graphId, graphToMove);
                LOGGER.warn("Error trying to update tables.", e);
            }
        }
        final GraphConfig configWithNewGraphId = cloneGraphConfigWithNewGraphId(newGraphId, graphToMove);
        // add the graph being renamed.
        GraphSerialisable newGraphSerialisable = new GraphSerialisable.Builder().graph(graphToMove).config(configWithNewGraphId).build();
        this.put(newGraphSerialisable, key);
        // Update cache
        if (isCacheEnabled()) {
            try {
                // Overwrite cache = true because the graphLibrary should have thrown an error before this point.
                federatedStoreCache.addGraphToCache(newGraphSerialisable, key, true);
            } catch (final CacheOperationException e) {
                String s = "Contact Admin for recovery. Error occurred updating graphId. GraphStorage=updated, Cache=outdated graphId.";
                LOGGER.error(s + " graphStorage graphId:{} cache graphId:{}", newGraphId, graphId);
                throw new StorageException(s, e);
            }
            federatedStoreCache.deleteGraphFromCache(graphId);
        }
        rtn = true;
    } else {
        rtn = false;
    }
    return rtn;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) OverwritingException(uk.gov.gchq.gaffer.commonutil.exception.OverwritingException) CacheOperationException(uk.gov.gchq.gaffer.cache.exception.CacheOperationException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Graph(uk.gov.gchq.gaffer.graph.Graph) GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) CacheOperationException(uk.gov.gchq.gaffer.cache.exception.CacheOperationException) AccumuloStore(uk.gov.gchq.gaffer.accumulostore.AccumuloStore) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException)

Example 2 with StorageException

use of uk.gov.gchq.gaffer.federatedstore.exception.StorageException in project Gaffer by gchq.

the class FederatedGraphStorage method changeGraphAccess.

private boolean changeGraphAccess(final String graphId, final FederatedAccess newFederatedAccess, final Predicate<FederatedAccess> accessPredicate) throws StorageException {
    boolean rtn;
    final Graph graphToMove = getGraphToMove(graphId, accessPredicate);
    if (nonNull(graphToMove)) {
        // remove graph to be moved
        FederatedAccess oldAccess = null;
        for (final Entry<FederatedAccess, Set<Graph>> entry : storage.entrySet()) {
            entry.getValue().removeIf(graph -> graph.getGraphId().equals(graphId));
            oldAccess = entry.getKey();
        }
        // add the graph being moved.
        this.put(new GraphSerialisable.Builder().graph(graphToMove).build(), newFederatedAccess);
        if (isCacheEnabled()) {
            // Update cache
            try {
                federatedStoreCache.addGraphToCache(graphToMove, newFederatedAccess, true);
            } catch (final CacheOperationException e) {
                // TODO FS recovery
                String s = "Error occurred updating graphAccess. GraphStorage=updated, Cache=outdated. graphId:" + graphId;
                LOGGER.error(s + " graphStorage access:{} cache access:{}", newFederatedAccess, oldAccess);
                throw new StorageException(s, e);
            }
        }
        rtn = true;
    } else {
        rtn = false;
    }
    return rtn;
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) CacheOperationException(uk.gov.gchq.gaffer.cache.exception.CacheOperationException) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException)

Example 3 with StorageException

use of uk.gov.gchq.gaffer.federatedstore.exception.StorageException in project Gaffer by gchq.

the class FederatedGraphStorageTest method checkSchemaNotLeakedWhenAlreadyExistsUnderDifferentAccessWithOtherGraphs.

@Test
public void checkSchemaNotLeakedWhenAlreadyExistsUnderDifferentAccessWithOtherGraphs() throws Exception {
    // Given
    final String unusualType = "unusualType";
    final String groupEnt = "ent";
    final String groupEdge = "edg";
    Schema schemaNotToBeExposed = new Schema.Builder().type(unusualType, String.class).type(DIRECTED_EITHER, Boolean.class).entity(groupEnt, new SchemaEntityDefinition.Builder().vertex(unusualType).build()).edge(groupEdge, new SchemaEdgeDefinition.Builder().source(unusualType).destination(unusualType).directed(DIRECTED_EITHER).build()).build();
    final GraphSerialisable graph1 = new GraphSerialisable.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID_A).build()).properties(PROPERTIES).schema(schemaNotToBeExposed).build();
    graphStorage.put(graph1, access);
    final GraphSerialisable graph2 = new GraphSerialisable.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID_B).build()).schema(new Schema.Builder().merge(schemaNotToBeExposed).entity("e2", e2).type("string2", String.class).build()).properties(PROPERTIES).build();
    graphStorage.put(graph2, altAccess);
    // When / Then
    try {
        graphStorage.put(graph2, access);
    } catch (StorageException e) {
        assertEquals("Error adding graph " + GRAPH_ID_B + " to storage due to: " + String.format(FederatedGraphStorage.USER_IS_ATTEMPTING_TO_OVERWRITE, GRAPH_ID_B), e.getMessage());
        testNotLeakingContents(e, unusualType, groupEdge, groupEnt);
    }
}
Also used : GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) Schema(uk.gov.gchq.gaffer.store.schema.Schema) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) Test(org.junit.jupiter.api.Test)

Example 4 with StorageException

use of uk.gov.gchq.gaffer.federatedstore.exception.StorageException in project Gaffer by gchq.

the class FederatedGraphStorageTest method checkSchemaNotLeakedWhenOverwritingExistingGraph.

@Test
public void checkSchemaNotLeakedWhenOverwritingExistingGraph() throws Exception {
    // Given
    graphStorage.setGraphLibrary(mock(GraphLibrary.class));
    final String unusualType = "unusualType";
    final String groupEnt = "ent";
    final String groupEdge = "edg";
    Schema schemaNotToBeExposed = new Schema.Builder().type(unusualType, String.class).type(DIRECTED_EITHER, Boolean.class).entity(groupEnt, new SchemaEntityDefinition.Builder().vertex(unusualType).build()).edge(groupEdge, new SchemaEdgeDefinition.Builder().source(unusualType).destination(unusualType).directed(DIRECTED_EITHER).build()).build();
    final GraphSerialisable graph1 = new GraphSerialisable.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID_A).build()).properties(PROPERTIES).schema(schemaNotToBeExposed).build();
    graphStorage.put(graph1, access);
    final GraphSerialisable graph2 = new GraphSerialisable.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID_A).build()).schema(new Schema.Builder().entity("e2", e2).type("string2", String.class).build()).properties(PROPERTIES).build();
    // When / Then
    try {
        graphStorage.put(graph2, access);
        fail(EXCEPTION_EXPECTED);
    } catch (StorageException e) {
        assertEquals("Error adding graph " + GRAPH_ID_A + " to storage due to: " + String.format(FederatedGraphStorage.USER_IS_ATTEMPTING_TO_OVERWRITE, GRAPH_ID_A), e.getMessage());
        testNotLeakingContents(e, unusualType, groupEdge, groupEnt);
    }
}
Also used : GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) GraphLibrary(uk.gov.gchq.gaffer.store.library.GraphLibrary) Schema(uk.gov.gchq.gaffer.store.schema.Schema) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) Test(org.junit.jupiter.api.Test)

Example 5 with StorageException

use of uk.gov.gchq.gaffer.federatedstore.exception.StorageException in project Gaffer by gchq.

the class PredefinedFederatedStore method initialise.

@Override
public void initialise(final String graphId, final Schema schema, final StoreProperties properties) throws StoreException {
    HashMapGraphLibrary.clear();
    CacheServiceLoader.shutdown();
    ExecutorService.shutdown();
    super.initialise(graphId, schema, properties);
    // Accumulo store just contains edges
    try {
        addGraphs(null, User.UNKNOWN_USER_ID, false, new GraphSerialisable.Builder().config(new GraphConfig(ACCUMULO_GRAPH_WITH_EDGES)).schema(new Schema.Builder().merge(schema.clone()).entities(Collections.emptyMap()).build()).properties(PROPERTIES).build());
        // Accumulo store just contains entities
        addGraphs(null, User.UNKNOWN_USER_ID, false, new GraphSerialisable.Builder().config(new GraphConfig(ACCUMULO_GRAPH_WITH_ENTITIES)).schema(new Schema.Builder().merge(schema.clone()).edges(Collections.emptyMap()).build()).properties(PROPERTIES).build());
    } catch (final StorageException e) {
        throw new StoreException(e.getMessage(), e);
    }
}
Also used : GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) Schema(uk.gov.gchq.gaffer.store.schema.Schema) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) StoreException(uk.gov.gchq.gaffer.store.StoreException)

Aggregations

StorageException (uk.gov.gchq.gaffer.federatedstore.exception.StorageException)10 GraphSerialisable (uk.gov.gchq.gaffer.graph.GraphSerialisable)8 Schema (uk.gov.gchq.gaffer.store.schema.Schema)5 Test (org.junit.jupiter.api.Test)4 CacheOperationException (uk.gov.gchq.gaffer.cache.exception.CacheOperationException)3 Graph (uk.gov.gchq.gaffer.graph.Graph)3 GraphConfig (uk.gov.gchq.gaffer.graph.GraphConfig)3 OperationException (uk.gov.gchq.gaffer.operation.OperationException)3 SchemaEntityDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 OverwritingException (uk.gov.gchq.gaffer.commonutil.exception.OverwritingException)2 SchemaException (uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException)2 StoreException (uk.gov.gchq.gaffer.store.StoreException)2 GraphLibrary (uk.gov.gchq.gaffer.store.library.GraphLibrary)2 Connector (org.apache.accumulo.core.client.Connector)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 AccumuloProperties (uk.gov.gchq.gaffer.accumulostore.AccumuloProperties)1 AccumuloStore (uk.gov.gchq.gaffer.accumulostore.AccumuloStore)1