|
1 | 1 | package io.dgraph.example; |
2 | 2 |
|
3 | | -import io.dgraph.DgraphProto.Mutation; |
4 | | -import io.dgraph.DgraphProto.Request; |
5 | 3 | import com.google.gson.Gson; |
6 | | -import io.dgraph.DgraphProto.Response; |
7 | 4 | import com.google.protobuf.ByteString; |
8 | 5 | import io.dgraph.DgraphClient; |
| 6 | +import io.dgraph.DgraphProto.Mutation; |
| 7 | +import io.dgraph.DgraphProto.Request; |
| 8 | +import io.dgraph.DgraphProto.Response; |
9 | 9 | import io.dgraph.Transaction; |
10 | 10 | import io.dgraph.TxnConflictException; |
11 | 11 | import java.util.Collections; |
12 | 12 | import java.util.Map; |
13 | 13 |
|
14 | 14 | public class MultiThreadedMutation implements Runnable { |
15 | | - // maximum retries |
16 | | - static final int MAX_RETRY_COUNT = 5; |
17 | | - static Integer globalThreadNumberCount = 1; |
18 | | - int threadNumber = 0; |
19 | | - // |
20 | | - private DgraphClient dgraphClient; |
21 | | - private Transaction txn; |
22 | | - |
23 | | - public MultiThreadedMutation(DgraphClient dgraphClient) { |
24 | | - //assign a thread number |
25 | | - synchronized (globalThreadNumberCount) { |
26 | | - this.threadNumber = globalThreadNumberCount++; |
27 | | - this.dgraphClient = dgraphClient; |
28 | | - } |
29 | | - |
30 | | - } |
| 15 | + // maximum retries |
| 16 | + static final int MAX_RETRY_COUNT = 5; |
| 17 | + static Integer globalThreadNumberCount = 1; |
| 18 | + int threadNumber = 0; |
| 19 | + // |
| 20 | + private DgraphClient dgraphClient; |
| 21 | + private Transaction txn; |
31 | 22 |
|
32 | | - public void run() { |
33 | | - boolean successFlag = false; |
34 | | - int retryCount = 0; |
35 | | - while (retryCount < MAX_RETRY_COUNT) { |
36 | | - try { |
37 | | - //fire the mutation and check for exceptions |
38 | | - doMutation(); |
39 | | - successFlag = true; |
40 | | - System.out.println(System.currentTimeMillis() + " Thread #" + threadNumber + " succeeded after " |
41 | | - + retryCount + " retries"); |
42 | | - break; |
43 | | - } catch (TxnConflictException txnConflictException) { |
44 | | - try { |
45 | | - System.out.println(System.currentTimeMillis() + " Thread #" + threadNumber |
46 | | - + " found a concurrent modification conflict, sleeping for 1 second..."); |
47 | | - Thread.sleep(1000); |
48 | | - System.out.println(System.currentTimeMillis() + " Thread #" + threadNumber + " resuming"); |
49 | | - } catch (InterruptedException e) { |
50 | | - e.printStackTrace(); |
51 | | - } |
52 | | - retryCount++; |
53 | | - } catch (Exception e) { |
54 | | - // cannot retry |
55 | | - e.printStackTrace(); |
56 | | - break; |
57 | | - } |
58 | | - } |
59 | | - //check if maximum retries has been crossed |
60 | | - if (!successFlag && retryCount >= MAX_RETRY_COUNT) { |
61 | | - System.out.println(System.currentTimeMillis() + " Thread #" + threadNumber + " giving up transaction after " |
62 | | - + (retryCount - 1) + " retries"); |
63 | | - } |
64 | | - } |
| 23 | + public MultiThreadedMutation(DgraphClient dgraphClient) { |
| 24 | + // assign a thread number |
| 25 | + synchronized (globalThreadNumberCount) { |
| 26 | + this.threadNumber = globalThreadNumberCount++; |
| 27 | + this.dgraphClient = dgraphClient; |
| 28 | + } |
| 29 | + } |
65 | 30 |
|
66 | | - private void doMutation() throws Exception { |
67 | | - txn = dgraphClient.newTransaction(); |
68 | | - Gson gson = new Gson(); |
69 | | - // Query |
70 | | - String query = "query all($a: string){\n" + " all(func: eq(name, $a)) {\n" + " " + "uid\n" + "name\n" |
71 | | - + "clickCount\n" + " }\n" + "}\n"; |
| 31 | + public void run() { |
| 32 | + boolean successFlag = false; |
| 33 | + int retryCount = 0; |
| 34 | + while (retryCount < MAX_RETRY_COUNT) { |
| 35 | + try { |
| 36 | + // fire the mutation and check for exceptions |
| 37 | + doMutation(); |
| 38 | + successFlag = true; |
| 39 | + System.out.println( |
| 40 | + System.currentTimeMillis() |
| 41 | + + " Thread #" |
| 42 | + + threadNumber |
| 43 | + + " succeeded after " |
| 44 | + + retryCount |
| 45 | + + " retries"); |
| 46 | + break; |
| 47 | + } catch (TxnConflictException txnConflictException) { |
| 48 | + try { |
| 49 | + System.out.println( |
| 50 | + System.currentTimeMillis() |
| 51 | + + " Thread #" |
| 52 | + + threadNumber |
| 53 | + + " found a concurrent modification conflict, sleeping for 1 second..."); |
| 54 | + Thread.sleep(1000); |
| 55 | + System.out.println(System.currentTimeMillis() + " Thread #" + threadNumber + " resuming"); |
| 56 | + } catch (InterruptedException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + retryCount++; |
| 60 | + } catch (Exception e) { |
| 61 | + // cannot retry |
| 62 | + e.printStackTrace(); |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + // check if maximum retries has been crossed |
| 67 | + if (!successFlag && retryCount >= MAX_RETRY_COUNT) { |
| 68 | + System.out.println( |
| 69 | + System.currentTimeMillis() |
| 70 | + + " Thread #" |
| 71 | + + threadNumber |
| 72 | + + " giving up transaction after " |
| 73 | + + (retryCount - 1) |
| 74 | + + " retries"); |
| 75 | + } |
| 76 | + } |
72 | 77 |
|
73 | | - Map<String, String> vars = Collections.singletonMap("$a", "Alice"); |
| 78 | + private void doMutation() throws Exception { |
| 79 | + txn = dgraphClient.newTransaction(); |
| 80 | + Gson gson = new Gson(); |
| 81 | + // Query |
| 82 | + String query = |
| 83 | + "query all($a: string){\n" |
| 84 | + + " all(func: eq(name, $a)) {\n" |
| 85 | + + " " |
| 86 | + + "uid\n" |
| 87 | + + "name\n" |
| 88 | + + "clickCount\n" |
| 89 | + + " }\n" |
| 90 | + + "}\n"; |
74 | 91 |
|
75 | | - Response response = dgraphClient.newReadOnlyTransaction().queryWithVars(query, vars); |
76 | | - People ppl = gson.fromJson(response.getJson().toStringUtf8(), People.class); |
77 | | - // |
78 | | - for (Person person : ppl.all) { |
79 | | - System.out.println(System.currentTimeMillis() + " Thread #" + threadNumber |
80 | | - + " increasing clickCount for uid " + person.uid + ", Name: " + person.name); |
81 | | - //increment clickCount |
82 | | - person.clickCount = person.clickCount + 1; |
| 92 | + Map<String, String> vars = Collections.singletonMap("$a", "Alice"); |
83 | 93 |
|
84 | | - try { |
85 | | - //find and update alice's clickCount in a transaction |
86 | | - String upsertQuery = "query {\n" + "user as var(func: eq(name, \"" + person.name + "\"))\n" + "}\n"; |
87 | | - Mutation mu2 = Mutation.newBuilder() |
88 | | - .setSetNquads(ByteString.copyFromUtf8("uid(user) <clickCount> \"" + person.clickCount + "\" .")) |
89 | | - .build(); |
| 94 | + Response response = dgraphClient.newReadOnlyTransaction().queryWithVars(query, vars); |
| 95 | + People ppl = gson.fromJson(response.getJson().toStringUtf8(), People.class); |
| 96 | + // |
| 97 | + for (Person person : ppl.all) { |
| 98 | + System.out.println( |
| 99 | + System.currentTimeMillis() |
| 100 | + + " Thread #" |
| 101 | + + threadNumber |
| 102 | + + " increasing clickCount for uid " |
| 103 | + + person.uid |
| 104 | + + ", Name: " |
| 105 | + + person.name); |
| 106 | + // increment clickCount |
| 107 | + person.clickCount = person.clickCount + 1; |
90 | 108 |
|
91 | | - Request request = Request.newBuilder().setQuery(upsertQuery).addMutations(mu2).setCommitNow(true) |
92 | | - .build(); |
93 | | - txn.doRequest(request); |
94 | | - txn.close(); |
95 | | - } catch (Exception ex) { |
96 | | - // if its a conflict exception, we can retry |
97 | | - if (ex.getCause().getCause() instanceof TxnConflictException) { |
98 | | - TxnConflictException txnConflictException = (TxnConflictException) ex.getCause().getCause(); |
99 | | - txn.close(); |
100 | | - throw (txnConflictException); |
101 | | - } else { |
102 | | - throw ex; |
103 | | - } |
| 109 | + try { |
| 110 | + // find and update alice's clickCount in a transaction |
| 111 | + String upsertQuery = |
| 112 | + "query {\n" + "user as var(func: eq(name, \"" + person.name + "\"))\n" + "}\n"; |
| 113 | + Mutation mu2 = |
| 114 | + Mutation.newBuilder() |
| 115 | + .setSetNquads( |
| 116 | + ByteString.copyFromUtf8( |
| 117 | + "uid(user) <clickCount> \"" + person.clickCount + "\" .")) |
| 118 | + .build(); |
104 | 119 |
|
105 | | - } finally { |
106 | | - txn.discard(); |
107 | | - } |
| 120 | + Request request = |
| 121 | + Request.newBuilder().setQuery(upsertQuery).addMutations(mu2).setCommitNow(true).build(); |
| 122 | + txn.doRequest(request); |
| 123 | + txn.close(); |
| 124 | + } catch (Exception ex) { |
| 125 | + // if its a conflict exception, we can retry |
| 126 | + if (ex.getCause().getCause() instanceof TxnConflictException) { |
| 127 | + TxnConflictException txnConflictException = |
| 128 | + (TxnConflictException) ex.getCause().getCause(); |
| 129 | + txn.close(); |
| 130 | + throw (txnConflictException); |
| 131 | + } else { |
| 132 | + throw ex; |
| 133 | + } |
108 | 134 |
|
109 | | - } |
110 | | - } |
| 135 | + } finally { |
| 136 | + txn.discard(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
111 | 140 | } |
0 commit comments