You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@geode.apache.org by "Jason Huynh (JIRA)" <ji...@apache.org> on 2015/07/20 21:22:04 UTC

[jira] [Created] (GEODE-146) Queries are not thread safe due to scopeId variable

Jason Huynh created GEODE-146:
---------------------------------

             Summary: Queries are not thread safe due to scopeId variable
                 Key: GEODE-146
                 URL: https://issues.apache.org/jira/browse/GEODE-146
             Project: Geode
          Issue Type: Bug
            Reporter: Jason Huynh
            Assignee: Jason Huynh


Currently queries are not thread safe although they should be.  This is due to the way the scopeId is stored in the CompiledSelect.  The fix consists of storing the scopeId in the execution context.

public void testThreadSafetyOfCompiledSelectScopeId() throws Exception {
    Cache cache = CacheUtils.getCache();
    RegionFactory<Integer, Portfolio> rf = cache
        .createRegionFactory(RegionShortcut.PARTITION);
    Region r = rf.create("keyzset");
    for (int i = 0; i < 100; i++) {
      r.put(i, new Portfolio(i));
    }
    ScopeThreadingTestHook scopeIDTestHook = new ScopeThreadingTestHook(3);
    DefaultQuery.testHook = scopeIDTestHook;
    QueryService qs = cache.getQueryService();
    Query q = qs
        .newQuery("SELECT DISTINCT * FROM /keyzset.keySet key WHERE key.id > 0 AND key.id <= 0 ORDER BY key asc LIMIT $3");
    Thread q1 = new Thread(new QueryRunnable(q, new Object[] { 10, 20, 10 }));
    Thread q2 = new Thread(new QueryRunnable(q, new Object[] { 5, 10, 5 }));
    Thread q3 = new Thread(new QueryRunnable(q, new Object[] { 2, 10, 8 }));
    q1.start();
    q2.start();
    q3.start();
    q1.join();
    q2.join();
    q3.join();
    assertEquals("Exceptions were thrown due to DefaultQuery not being thread-safe", true, scopeIDTestHook.isOk());
  }

  private class QueryRunnable implements Runnable {
    private Query q;
    private Object[] params;

    public QueryRunnable(Query q, Object[] params) {
      this.q = q;
      this.params = params;
    }
    public void run() {
      try {
        q.execute(params);
      } catch (Exception e) {
        throw new TestException("exception occured while executing query", e);
      }
    }
  }

  public class ScopeThreadingTestHook implements DefaultQuery.TestHook {
    private CyclicBarrier barrier;
    private List<Exception> exceptionsThrown = new LinkedList<Exception>();

    public ScopeThreadingTestHook(int numThreads) {
      barrier = new CyclicBarrier(numThreads);
    }

    @Override
    public void doTestHook(int spot) {
      this.doTestHook(spot + "");
    }
    @Override
    public void doTestHook(String spot) {
      if (spot.equals("1")) {
        try {
          barrier.await(8, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
          exceptionsThrown.add(e);
          Thread.currentThread().interrupt();
        } catch (Exception e) {
          exceptionsThrown.add(e);
        }
      }
    }
    public boolean isOk() {
      return exceptionsThrown.size() == 0;
    }
  }



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)