-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support task auto manager by server role
- Loading branch information
Showing
12 changed files
with
480 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
hugegraph-api/src/main/java/org/apache/hugegraph/core/RoleTypeDataAdapterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.hugegraph.core; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.apache.hugegraph.HugeGraphParams; | ||
import org.apache.hugegraph.auth.SchemaDefine; | ||
import org.apache.hugegraph.backend.query.ConditionQuery; | ||
import org.apache.hugegraph.backend.store.BackendEntry; | ||
import org.apache.hugegraph.backend.tx.GraphTransaction; | ||
import org.apache.hugegraph.election.RoleTypeData; | ||
import org.apache.hugegraph.election.RoleTypeDataAdapter; | ||
import org.apache.hugegraph.schema.VertexLabel; | ||
import org.apache.hugegraph.structure.HugeVertex; | ||
import org.apache.hugegraph.type.HugeType; | ||
import org.apache.hugegraph.type.define.DataType; | ||
import org.apache.hugegraph.type.define.HugeKeys; | ||
import org.apache.tinkerpop.gremlin.structure.Graph; | ||
import org.apache.tinkerpop.gremlin.structure.T; | ||
import org.apache.tinkerpop.gremlin.structure.Vertex; | ||
|
||
public class RoleTypeDataAdapterImpl implements RoleTypeDataAdapter { | ||
|
||
private final HugeGraphParams graphParams; | ||
private final Schema schema; | ||
|
||
public RoleTypeDataAdapterImpl(HugeGraphParams graphParams) { | ||
this.graphParams = graphParams; | ||
this.schema = new Schema(graphParams); | ||
this.schema.initSchemaIfNeeded(); | ||
} | ||
|
||
@Override | ||
public boolean updateIfNodePresent(RoleTypeData stateData) { | ||
// if epoch increase, update and return true | ||
// if epoch equal, ignore different node, return false | ||
try { | ||
GraphTransaction tx = this.graphParams.systemTransaction(); | ||
tx.doUpdateIfPresent(this.constructEntry(stateData)); | ||
tx.commitOrRollback(); | ||
} catch (Throwable ignore){ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
BackendEntry constructEntry(RoleTypeData stateData) { | ||
List<Object> list = new ArrayList<>(8); | ||
list.add(P.LABEL); | ||
list.add(P.ROLE_DATA); | ||
|
||
list.add(P.NODE); | ||
list.add(stateData.node()); | ||
|
||
list.add(P.CLOCK); | ||
list.add(stateData.clock()); | ||
|
||
list.add(P.EPOCH); | ||
list.add(stateData.epoch()); | ||
|
||
list.add(P.TYPE); | ||
list.add("default"); | ||
|
||
HugeVertex vertex = this.graphParams.systemTransaction() | ||
.constructVertex(false, list); | ||
|
||
return this.graphParams.serializer().writeVertex(vertex); | ||
} | ||
|
||
@Override | ||
public Optional<RoleTypeData> query() { | ||
GraphTransaction tx = this.graphParams.systemTransaction(); | ||
ConditionQuery query = new ConditionQuery(HugeType.VERTEX); | ||
VertexLabel vl = this.graphParams.graph().vertexLabel(P.ROLE_DATA); | ||
query.eq(HugeKeys.LABEL, vl.id()); | ||
query.eq(HugeKeys.PROPERTY_KEY, "default"); | ||
query.showHidden(true); | ||
Iterator<Vertex> vertexIterator = tx.queryVertices(query); | ||
if (vertexIterator.hasNext()) { | ||
Vertex next = vertexIterator.next(); | ||
String node = (String) next.property(P.NODE).value(); | ||
Long clock = (Long) next.property(P.CLOCK).value(); | ||
Integer epoch = (Integer) next.property(P.EPOCH).value(); | ||
|
||
RoleTypeData roleTypeData = new RoleTypeData(node, epoch, clock); | ||
|
||
return Optional.of(roleTypeData); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
public static final class P { | ||
|
||
public static final String ROLE_DATA = Graph.Hidden.hide("role_data"); | ||
|
||
public static final String LABEL = T.label.getAccessor(); | ||
|
||
public static final String NODE = Graph.Hidden.hide("role_node"); | ||
|
||
public static final String CLOCK = Graph.Hidden.hide("role_clock"); | ||
|
||
public static final String EPOCH = Graph.Hidden.hide("role_epoch"); | ||
|
||
public static final String TYPE = Graph.Hidden.hide("role_type"); | ||
} | ||
|
||
|
||
public static final class Schema extends SchemaDefine { | ||
|
||
public Schema(HugeGraphParams graph) { | ||
super(graph, P.ROLE_DATA); | ||
} | ||
|
||
@Override | ||
public void initSchemaIfNeeded() { | ||
if (this.existVertexLabel(this.label)) { | ||
return; | ||
} | ||
|
||
String[] properties = this.initProperties(); | ||
|
||
VertexLabel label = this.schema().vertexLabel(this.label) | ||
.enableLabelIndex(true) | ||
.usePrimaryKeyId() | ||
.primaryKeys(P.TYPE) | ||
.properties(properties) | ||
.build(); | ||
this.graph.schemaTransaction().addVertexLabel(label); | ||
} | ||
|
||
private String[] initProperties() { | ||
List<String> props = new ArrayList<>(); | ||
|
||
props.add(createPropertyKey(P.NODE, DataType.TEXT)); | ||
props.add(createPropertyKey(P.CLOCK, DataType.LONG)); | ||
props.add(createPropertyKey(P.EPOCH, DataType.INT)); | ||
props.add(createPropertyKey(P.TYPE, DataType.TEXT)); | ||
|
||
return super.initProperties(props); | ||
} | ||
} | ||
} |
Oops, something went wrong.