Search in sources :

Example 1 with OrientGraph

use of org.apache.tinkerpop.gremlin.orientdb.OrientGraph in project open-kilda by telstra.

the class OrientDbGraphFactory method getGraph.

/**
 * Returns an instance of framed graph which is bound to the current persistence context.
 * Create a new one if there's no such.
 */
@Override
public DelegatingFramedGraph<OrientGraph> getGraph() {
    Connect effectiveConnect = getConnectCreateIfMissing();
    OrientGraphFactory factory = effectiveConnect.getFactory();
    log.debug("Opening a framed graph for {}", factory);
    OrientGraph orientGraph = Failsafe.with(newPoolAcquireRetryPolicy()).get(() -> {
        OrientGraph obtainedGraph = factory.getTx();
        validateGraph(obtainedGraph);
        return obtainedGraph;
    });
    log.debug("OrientGraph instance has been created: {}", orientGraph);
    return new DelegatingFramedGraph<>(orientGraph, effectiveConnect.getBuilder(), effectiveConnect.getTypeResolver());
}
Also used : DelegatingFramedGraph(com.syncleus.ferma.DelegatingFramedGraph) OrientGraph(org.apache.tinkerpop.gremlin.orientdb.OrientGraph) OrientGraphFactory(org.apache.tinkerpop.gremlin.orientdb.OrientGraphFactory)

Example 2 with OrientGraph

use of org.apache.tinkerpop.gremlin.orientdb.OrientGraph in project open-kilda by telstra.

the class OrientDbIslRepository method findActiveByPathAndBandwidthAndEncapsulationType.

@Override
public Collection<IslImmutableView> findActiveByPathAndBandwidthAndEncapsulationType(PathId pathId, long requiredBandwidth, FlowEncapsulationType flowEncapsulationType) {
    Map<String, String> switches = findActiveSwitchesAndPopByEncapsulationType(flowEncapsulationType);
    OrientGraph orientGraph = graphSupplier.get();
    Map<IslEndpoints, Long> segments = new HashMap<>();
    try (OGremlinResultSet results = orientGraph.querySql(QUERY_FETCH_SEGMENTS_BY_PATH, PathIdConverter.INSTANCE.toGraphProperty(pathId))) {
        results.forEach(gs -> {
            String srcSwitch = gs.getProperty(PathSegmentFrame.SRC_SWITCH_ID_PROPERTY);
            String dstSwitch = gs.getProperty(PathSegmentFrame.DST_SWITCH_ID_PROPERTY);
            if (switches.containsKey(srcSwitch) && switches.containsKey(dstSwitch)) {
                segments.put(new IslEndpoints(srcSwitch, gs.getProperty(PathSegmentFrame.SRC_PORT_PROPERTY), dstSwitch, gs.getProperty(PathSegmentFrame.DST_PORT_PROPERTY)), gs.getProperty(PathSegmentFrame.BANDWIDTH_PROPERTY));
            }
        });
    }
    String islStatusAsStr = IslStatusConverter.INSTANCE.toGraphProperty(IslStatus.ACTIVE);
    List<IslImmutableView> isls = new ArrayList<>(segments.size());
    segments.keySet().forEach(endpoint -> {
        try (OGremlinResultSet results = orientGraph.querySql(QUERY_FETCH_ISLS_BY_ENDPOINTS_AND_BANDWIDTH, endpoint.getSrcSwitch(), endpoint.getSrcPort(), endpoint.getDestSwitch(), endpoint.getDestPort(), islStatusAsStr, requiredBandwidth - segments.get(endpoint))) {
            results.forEach(gs -> isls.add(mapToIslImmutableView(gs, switches.get(endpoint.getSrcSwitch()), switches.get(endpoint.getDestSwitch()))));
        }
    });
    return isls;
}
Also used : OrientGraph(org.apache.tinkerpop.gremlin.orientdb.OrientGraph) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OGremlinResultSet(org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet)

Example 3 with OrientGraph

use of org.apache.tinkerpop.gremlin.orientdb.OrientGraph in project open-kilda by telstra.

the class GraphSupplier method get.

@Override
public OrientGraph get() {
    PersistenceContext context = PersistenceContextManager.INSTANCE.getContextCreateIfMissing();
    OrientDbContextExtension contextExtension = implementation.getContextExtension(context);
    DelegatingFramedGraph<OrientGraph> wrapper = contextExtension.getGraphCreateIfMissing();
    if (wrapper == null) {
        throw new PersistenceException("Failed to obtain a framed graph");
    }
    return wrapper.getBaseGraph();
}
Also used : OrientGraph(org.apache.tinkerpop.gremlin.orientdb.OrientGraph) OrientDbContextExtension(org.openkilda.persistence.orientdb.OrientDbContextExtension) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PersistenceContext(org.openkilda.persistence.context.PersistenceContext)

Example 4 with OrientGraph

use of org.apache.tinkerpop.gremlin.orientdb.OrientGraph in project open-kilda by telstra.

the class OrientDbPersistenceImplementation method onContextClose.

@Override
public void onContextClose(PersistenceContext context) {
    OrientDbContextExtension contextExtension = getContextExtension(context);
    DelegatingFramedGraph<OrientGraph> currentGraph = contextExtension.removeGraph();
    if (currentGraph != null) {
        String threadName = Thread.currentThread().getName();
        // Commit an implicit transaction to release graph resources.
        try {
            log.trace("Committing a transaction on the graph: {} in {}", currentGraph, threadName);
            currentGraph.getBaseGraph().commit();
        } catch (Exception e) {
            log.error("Failed to commit a transaction in {}", threadName, e);
        }
        try {
            log.trace("Closing the framed graph: {} in {}", currentGraph, threadName);
            currentGraph.close();
        } catch (IOException e) {
            throw new PersistenceException(String.format("Failed to close graph in %s", threadName), e);
        }
    }
}
Also used : OrientGraph(org.apache.tinkerpop.gremlin.orientdb.OrientGraph) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) IOException(java.io.IOException) IOException(java.io.IOException) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Aggregations

OrientGraph (org.apache.tinkerpop.gremlin.orientdb.OrientGraph)4 PersistenceException (org.openkilda.persistence.exceptions.PersistenceException)2 DelegatingFramedGraph (com.syncleus.ferma.DelegatingFramedGraph)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 OrientGraphFactory (org.apache.tinkerpop.gremlin.orientdb.OrientGraphFactory)1 OGremlinResultSet (org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet)1 PersistenceContext (org.openkilda.persistence.context.PersistenceContext)1 OrientDbContextExtension (org.openkilda.persistence.orientdb.OrientDbContextExtension)1