- Notifications
You must be signed in to change notification settings - Fork 45
implement aggregator module #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits Select commit Hold shift + click to select a range
94d4f22 implement aggregator module
javeme 3a9d2b5 add DefaultAggregator.newValue()
javeme e1d6812 integrate aggregator into master-service and worker-service
javeme 02c8191 ensure worker init() executed after master
javeme 99acf6a add rpc interface registeredAggregators()
javeme 3acffaa add repair() for Aggregator
javeme 00762f5 let master context use config of outer class
javeme a54d9ad fix test error
javeme 1d32448 add aggregator test
javeme 3479657 test for multi workers
javeme e75b814 fix style
javeme 25c0b1e tiny fix
javeme 51cd76d support register aggregator with default value
javeme 4a63796 add more error check and fix/improve some code
javeme acdc777 add more test and error test
javeme 9b1646d fix WorkerServiceTest process to improve fault tolerance
javeme 431486a remove master clean() code before test (done in master init())
javeme 54e7833 add Manager.inited()
javeme 5493b60 remove worker_id option and use server_port instead
javeme 6e49914 use rpc-url from master-info
javeme 13620f9 tiny improve
javeme 6c41731 improve interface java-doc
javeme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions 80 computer-core/src/main/java/com/baidu/hugegraph/computer/core/aggregator/Aggregators.java
This file contains hidden or 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,80 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * 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 com.baidu.hugegraph.computer.core.aggregator; | ||
| | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| | ||
| import com.baidu.hugegraph.computer.core.graph.value.Value; | ||
| import com.baidu.hugegraph.computer.core.rpc.AggregateRpcService; | ||
| import com.baidu.hugegraph.util.E; | ||
| import com.google.common.collect.ImmutableMap; | ||
| | ||
| public class Aggregators { | ||
| | ||
| private final Map<String, Aggregator<? extends Value<?>>> aggregators; | ||
| | ||
| public Aggregators() { | ||
| this(ImmutableMap.of()); | ||
| } | ||
| | ||
| public Aggregators(Map<String, Aggregator<?>> aggrs) { | ||
| this.aggregators = new ConcurrentHashMap<>(aggrs); | ||
| } | ||
| | ||
| public Map<String, Value<?>> values() { | ||
| Map<String, Value<?>> values = new HashMap<>(); | ||
| for (Entry<String, Aggregator<? extends Value<?>>> aggr : | ||
| this.aggregators.entrySet()) { | ||
| values.put(aggr.getKey(), aggr.getValue().aggregatedValue()); | ||
| } | ||
| return values; | ||
| } | ||
| | ||
| public <V extends Value<?>> Aggregator<V> get(String name, | ||
| AggregateRpcService service) { | ||
| Aggregator<?> aggregator = this.aggregators.get(name); | ||
| if (aggregator == null) { | ||
| if (service != null) { | ||
| // Try to get the aggregator maybe created dynamic | ||
| aggregator = service.getAggregator(name); | ||
| if (aggregator != null) { | ||
| this.aggregators.put(name, aggregator); | ||
| } | ||
| } | ||
| E.checkArgument(aggregator != null, | ||
| "Can't get aggregator '%s'", name); | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| Aggregator<V> result = (Aggregator<V>) aggregator; | ||
| return result; | ||
| } | ||
| | ||
| public void reset(RegisterAggregators register) { | ||
| this.aggregators.clear(); | ||
| this.aggregators.putAll(register.copyAll()); | ||
| } | ||
| | ||
| public void clear() { | ||
| this.aggregators.clear(); | ||
| } | ||
| } |
177 changes: 177 additions & 0 deletions 177 ...er-core/src/main/java/com/baidu/hugegraph/computer/core/aggregator/DefaultAggregator.java
This file contains hidden or 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,177 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * 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 com.baidu.hugegraph.computer.core.aggregator; | ||
| | ||
| import com.baidu.hugegraph.computer.core.combiner.Combiner; | ||
| import com.baidu.hugegraph.computer.core.common.ComputerContext; | ||
| import com.baidu.hugegraph.computer.core.common.exception.ComputerException; | ||
| import com.baidu.hugegraph.computer.core.graph.value.DoubleValue; | ||
| import com.baidu.hugegraph.computer.core.graph.value.FloatValue; | ||
| import com.baidu.hugegraph.computer.core.graph.value.IntValue; | ||
| import com.baidu.hugegraph.computer.core.graph.value.LongValue; | ||
| import com.baidu.hugegraph.computer.core.graph.value.Value; | ||
| import com.baidu.hugegraph.computer.core.graph.value.ValueType; | ||
| import com.baidu.hugegraph.util.E; | ||
| | ||
| public class DefaultAggregator<V extends Value<?>> implements Aggregator<V> { | ||
| | ||
| private final ValueType type; | ||
| private final Class<? extends Combiner<V>> combinerClass; | ||
| | ||
| private transient Combiner<V> combiner; | ||
| private transient ThreadLocal<V> localValue; | ||
| | ||
| private V value; | ||
| | ||
| public DefaultAggregator(ComputerContext context, ValueType type, | ||
| Class<? extends Combiner<V>> combinerClass, | ||
| V defaultValue) { | ||
| E.checkArgument(type != null, | ||
| "The value type of aggregator can't be null"); | ||
| E.checkArgument(combinerClass != null, | ||
| "The combiner of aggregator can't be null"); | ||
| this.type = type; | ||
| this.combinerClass = combinerClass; | ||
| | ||
| if (defaultValue != null) { | ||
| this.checkValue(defaultValue); | ||
| } | ||
| this.value = defaultValue; | ||
| | ||
| if (context != null) { | ||
| this.repair(context); | ||
| } | ||
| | ||
| E.checkArgument(this.value != null, | ||
| "Must provide default value for aggregator"); | ||
| } | ||
| | ||
| @Override | ||
| public void aggregateValue(V value) { | ||
| this.checkValue(value); | ||
| this.value = this.combiner.combine(value, this.value); | ||
| } | ||
| | ||
| @Override | ||
| public void aggregateValue(int value) { | ||
| assert this.type == ValueType.INT; | ||
| V localValue = this.localValue.get(); | ||
| ((IntValue) localValue).value(value); | ||
| this.combineAndSwapIfNeeded(localValue, this.value); | ||
| } | ||
| | ||
| @Override | ||
| public void aggregateValue(long value) { | ||
| assert this.type == ValueType.LONG; | ||
| V localValue = this.localValue.get(); | ||
| ((LongValue) localValue).value(value); | ||
| this.combineAndSwapIfNeeded(localValue, this.value); | ||
| } | ||
| | ||
| @Override | ||
| public void aggregateValue(float value) { | ||
| assert this.type == ValueType.FLOAT; | ||
| V localValue = this.localValue.get(); | ||
| ((FloatValue) localValue).value(value); | ||
| this.combineAndSwapIfNeeded(localValue, this.value); | ||
| } | ||
| | ||
| @Override | ||
| public void aggregateValue(double value) { | ||
| assert this.type == ValueType.DOUBLE; | ||
| V localValue = this.localValue.get(); | ||
| ((DoubleValue) localValue).value(value); | ||
| this.combineAndSwapIfNeeded(localValue, this.value); | ||
| } | ||
| | ||
| private void combineAndSwapIfNeeded(V localValue, V thisValue) { | ||
| // TODO: call combine(localValue, thisValue, <output>thisValue) | ||
| V tmp = this.combiner.combine(localValue, thisValue); | ||
| if (tmp == localValue) { | ||
| this.localValue.set(thisValue); | ||
| this.value = tmp; | ||
| } else { | ||
| assert tmp == thisValue; | ||
| } | ||
| } | ||
| | ||
| @Override | ||
| public V aggregatedValue() { | ||
| assert this.value != null; | ||
| return this.value; | ||
| } | ||
| | ||
| @Override | ||
| public void aggregatedValue(V value) { | ||
| this.checkValue(value); | ||
| this.value = value; | ||
| } | ||
| | ||
| private void checkValue(V value) { | ||
| E.checkNotNull(value, "aggregator", "value"); | ||
| E.checkArgument(value.type() == this.type, | ||
| "Can't set %s value '%s' to %s aggregator", | ||
| value.type().string(), value, this.type.string()); | ||
| } | ||
| | ||
| @Override | ||
| public String toString() { | ||
| return this.value.toString(); | ||
| } | ||
| | ||
| @Override | ||
| public Aggregator<V> copy() { | ||
| DefaultAggregator<V> aggregator = new DefaultAggregator<>( | ||
| null, this.type, | ||
| this.combinerClass, | ||
| this.value); | ||
| // Ensure deep copy the value | ||
| @SuppressWarnings("unchecked") | ||
| V deepCopyValue = (V) this.value.copy(); | ||
| aggregator.value = deepCopyValue; | ||
| aggregator.combiner = this.combiner; | ||
| aggregator.localValue = this.localValue; | ||
| return aggregator; | ||
| } | ||
| | ||
| @Override | ||
| public void repair(ComputerContext context) { | ||
| try { | ||
| this.combiner = this.combinerClass.newInstance(); | ||
| } catch (Exception e) { | ||
| throw new ComputerException("Can't new instance from class: %s", | ||
| e, this.combinerClass.getName()); | ||
| } | ||
| | ||
| this.localValue = ThreadLocal.withInitial(() -> { | ||
| return this.newValue(context); | ||
| }); | ||
| | ||
| if (this.value == null) { | ||
| this.value = this.newValue(context); | ||
| } | ||
| } | ||
| | ||
| private V newValue(ComputerContext context) { | ||
| @SuppressWarnings("unchecked") | ||
| V val = (V) context.valueFactory().createValue(this.type); | ||
| return val; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is master and master-computation interchangeable?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean aslo add createAggregator() to Aggregator4Master?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the aggregator is registered by master-computation.