Skip to content

Commit 02fbe75

Browse files
release v20.03.2 (#159)
1 parent b7dce2a commit 02fbe75

File tree

8 files changed

+228
-209
lines changed

8 files changed

+228
-209
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ grab via Maven:
5454
<dependency>
5555
<groupId>io.dgraph</groupId>
5656
<artifactId>dgraph4j</artifactId>
57-
<version>20.03.1</version>
57+
<version>20.03.2</version>
5858
</dependency>
5959
```
6060
or Gradle:
6161
```groovy
62-
compile 'io.dgraph:dgraph4j:20.03.1'
62+
compile 'io.dgraph:dgraph4j:20.03.2'
6363
```
6464

6565
## Supported Versions
@@ -106,7 +106,7 @@ use a different version of this client.
106106
| 1.7.0 | 1.15.0 | 2.0.12.Final |
107107
| 1.7.3-1.7.5 | 1.15.1 | 2.0.12.Final |
108108
| 2.0.0-2.1.0 | 1.22.1 | 2.0.25.Final |
109-
| 20.03.0 | 1.26.0 | 2.0.26.Final |
109+
| 20.03.0-20.03.2 | 1.26.0 | 2.0.26.Final |
110110

111111
So, for example, if you were using `dgraph4j v20.03.0`, then you would need to use `2.0.26-Final`
112112
as the version for `netty-tcnative-boringssl-static` dependency as suggested by gRPC docs for

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ apply plugin: 'signing'
4141

4242
group = 'io.dgraph'
4343
archivesBaseName = 'dgraph4j'
44-
version = '20.03.1'
44+
version = '20.03.2'
4545
sourceCompatibility = 1.8
4646
targetCompatibility = 1.8
4747

Lines changed: 120 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,140 @@
11
package io.dgraph.example;
22

3-
import io.dgraph.DgraphProto.Mutation;
4-
import io.dgraph.DgraphProto.Request;
53
import com.google.gson.Gson;
6-
import io.dgraph.DgraphProto.Response;
74
import com.google.protobuf.ByteString;
85
import io.dgraph.DgraphClient;
6+
import io.dgraph.DgraphProto.Mutation;
7+
import io.dgraph.DgraphProto.Request;
8+
import io.dgraph.DgraphProto.Response;
99
import io.dgraph.Transaction;
1010
import io.dgraph.TxnConflictException;
1111
import java.util.Collections;
1212
import java.util.Map;
1313

1414
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;
3122

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+
}
6530

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+
}
7277

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";
7491

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");
8393

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;
90108

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();
104119

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+
}
108134

109-
}
110-
}
135+
} finally {
136+
txn.discard();
137+
}
138+
}
139+
}
111140
}

0 commit comments

Comments
 (0)