Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WorkerContext |
|
| 1.0;1 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
16 | * limitations under the License. | |
17 | */ | |
18 | ||
19 | package org.apache.giraph.graph; | |
20 | ||
21 | import org.apache.hadoop.io.Writable; | |
22 | import org.apache.hadoop.mapreduce.Mapper; | |
23 | ||
24 | /** | |
25 | * WorkerContext allows for the execution of user code | |
26 | * on a per-worker basis. There's one WorkerContext per worker. | |
27 | */ | |
28 | @SuppressWarnings("rawtypes") | |
29 | 24 | public abstract class WorkerContext implements WorkerAggregatorUsage { |
30 | /** Global graph state */ | |
31 | private GraphState graphState; | |
32 | ||
33 | /** | |
34 | * Set the graph state. | |
35 | * | |
36 | * @param graphState Used to set the graph state. | |
37 | */ | |
38 | public void setGraphState(GraphState graphState) { | |
39 | 218 | this.graphState = graphState; |
40 | 218 | } |
41 | ||
42 | /** | |
43 | * Initialize the WorkerContext. | |
44 | * This method is executed once on each Worker before the first | |
45 | * superstep starts. | |
46 | * | |
47 | * @throws IllegalAccessException Thrown for getting the class | |
48 | * @throws InstantiationException Expected instantiation in this method. | |
49 | */ | |
50 | public abstract void preApplication() throws InstantiationException, | |
51 | IllegalAccessException; | |
52 | ||
53 | /** | |
54 | * Finalize the WorkerContext. | |
55 | * This method is executed once on each Worker after the last | |
56 | * superstep ends. | |
57 | */ | |
58 | public abstract void postApplication(); | |
59 | ||
60 | /** | |
61 | * Execute user code. | |
62 | * This method is executed once on each Worker before each | |
63 | * superstep starts. | |
64 | */ | |
65 | public abstract void preSuperstep(); | |
66 | ||
67 | /** | |
68 | * Execute user code. | |
69 | * This method is executed once on each Worker after each | |
70 | * superstep ends. | |
71 | */ | |
72 | public abstract void postSuperstep(); | |
73 | ||
74 | /** | |
75 | * Retrieves the current superstep. | |
76 | * | |
77 | * @return Current superstep | |
78 | */ | |
79 | public long getSuperstep() { | |
80 | 82 | return graphState.getSuperstep(); |
81 | } | |
82 | ||
83 | /** | |
84 | * Get the total (all workers) number of vertices that | |
85 | * existed in the previous superstep. | |
86 | * | |
87 | * @return Total number of vertices (-1 if first superstep) | |
88 | */ | |
89 | public long getTotalNumVertices() { | |
90 | 122 | return graphState.getTotalNumVertices(); |
91 | } | |
92 | ||
93 | /** | |
94 | * Get the total (all workers) number of edges that | |
95 | * existed in the previous superstep. | |
96 | * | |
97 | * @return Total number of edges (-1 if first superstep) | |
98 | */ | |
99 | public long getTotalNumEdges() { | |
100 | 10 | return graphState.getTotalNumEdges(); |
101 | } | |
102 | ||
103 | /** | |
104 | * Get the mapper context | |
105 | * | |
106 | * @return Mapper context | |
107 | */ | |
108 | public Mapper.Context getContext() { | |
109 | 14 | return graphState.getContext(); |
110 | } | |
111 | ||
112 | @Override | |
113 | public <A extends Writable> void aggregate(String name, A value) { | |
114 | 0 | graphState.getGraphMapper().getWorkerAggregatorUsage(). |
115 | aggregate(name, value); | |
116 | 0 | } |
117 | ||
118 | @Override | |
119 | public <A extends Writable> A getAggregatedValue(String name) { | |
120 | 237 | return graphState.getGraphMapper().getWorkerAggregatorUsage(). |
121 | <A>getAggregatedValue(name); | |
122 | } | |
123 | } |