Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import com.orientechnologies.common.concur.ONeedRetryException;
  2. import com.orientechnologies.orient.core.config.OGlobalConfiguration;
  3. import com.orientechnologies.orient.core.id.ORID;
  4. import com.orientechnologies.orient.core.sql.executor.OResult;
  5. import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
  6. import org.apache.tinkerpop.gremlin.orientdb.OrientGraphFactory;
  7. import org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet;
  8. import org.apache.tinkerpop.gremlin.structure.Vertex;
  9. import org.junit.Assert;
  10. import org.junit.Test;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.concurrent.atomic.AtomicLong;
  15.  
  16. /**
  17. * Created by luigidellaquila on 27/07/17.
  18. */
  19. public class TestCommitRetry {
  20.  
  21. static int N_THREADS = 8;
  22. static int NUM_OF_BLOCKS = 100;
  23. static int VERTICES_PER_BLOCK = 10;
  24. static int NUM_OF_RETRIES = 200;
  25.  
  26. private ORID rootId;
  27.  
  28. public TestCommitRetry() {
  29. //set this threshold high, so that you have a high chance to have ONeedRetryException
  30. OGlobalConfiguration.RID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD.setValue(1000000);
  31. }
  32.  
  33. final AtomicLong recognizedFailures = new AtomicLong();
  34.  
  35. class ThreadCorrect extends Thread {
  36. private final OrientGraphFactory factory;
  37.  
  38. public ThreadCorrect(OrientGraphFactory factory) {
  39. this.factory = factory;
  40. }
  41.  
  42. @Override
  43. public void run() {
  44. OrientGraph db = factory.getTx();
  45.  
  46. for (int i = 0; i < NUM_OF_BLOCKS; i++) {
  47. boolean committed = false;
  48. db.begin();
  49. for (int j = 0; j < NUM_OF_RETRIES; j++) {
  50.  
  51. Vertex root = db.vertices(rootId).next(); // reload the root at each retry!!!
  52. try {
  53. createSubgraph(db, root);
  54. db.commit();
  55. //if everything is OK, stop retrying
  56. committed = true;
  57. break;
  58. } catch (ONeedRetryException ex) {
  59. //it case of error, rollback and retry the whole operation
  60. db.rollback();
  61. db.begin();
  62. }
  63. }
  64. if (!committed) {
  65. recognizedFailures.incrementAndGet();
  66. }
  67. }
  68. db.close();
  69. }
  70.  
  71. private void createSubgraph(OrientGraph db, Vertex root) {
  72. for (int j = 0; j < VERTICES_PER_BLOCK; j++) {
  73. Vertex vertex = db.addVertex("V");
  74. root.addEdge("E", vertex);
  75. }
  76. }
  77. }
  78.  
  79. @Test
  80. public void test() throws InterruptedException {
  81. final OrientGraphFactory factory = new OrientGraphFactory("memory:testCorrect");
  82.  
  83. //create the root vertex
  84. OrientGraph db = factory.getNoTx();
  85. Vertex root = db.addVertex("V");
  86. rootId = (ORID) root.id();
  87. db.close();
  88.  
  89. //run N_THREADS threads in parallel that do massive insert
  90. List<Thread> list = new ArrayList<>();
  91. for (int i = 0; i < N_THREADS; i++) {
  92. ThreadCorrect thread = new ThreadCorrect(factory);
  93. list.add(thread);
  94. thread.start();
  95. }
  96. for (Thread thread : list) {
  97. thread.join();
  98. }
  99.  
  100. //check that no thread notified a failure
  101. Assert.assertEquals(0, recognizedFailures.intValue());
  102. db = factory.getNoTx();
  103.  
  104. //check that there are actually all the records in the DB
  105. OGremlinResultSet rs = db.executeSql("SELECT count(*) as count FROM V");
  106. OResult item = rs.getRawResultSet().next();
  107. Assert.assertEquals((long) N_THREADS * NUM_OF_BLOCKS * VERTICES_PER_BLOCK + 1, (long) item.getProperty("count"));
  108. rs.close();
  109.  
  110. db.close();
  111. factory.close();
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement