Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 34effcf9bc408dcdc44332eb498bc3004c827715 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (c) 2010-2012, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Ibrahim Sallam - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.objectivity.db;

import org.eclipse.emf.cdo.server.internal.objectivity.bundle.OM;
import org.eclipse.emf.cdo.server.internal.objectivity.clustering.ObjyPlacementManager;
import org.eclipse.emf.cdo.server.internal.objectivity.clustering.ObjyPlacementManagerImpl;
import org.eclipse.emf.cdo.server.objectivity.IObjectivityStoreConfig;

import org.eclipse.net4j.util.om.trace.ContextTracer;

import com.objy.db.DatabaseNotFoundException;
import com.objy.db.DatabaseOpenException;
import com.objy.db.ObjyRuntimeException;
import com.objy.db.app.Connection;
import com.objy.db.app.Session;
import com.objy.db.app.oo;

import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class ObjyConnection
{

  public static final ObjyConnection INSTANCE = new ObjyConnection();

  protected Connection connection = null;

  protected boolean isConnected = false;

  protected String fdName = "";

  // protected ObjectivityStore store = null;

  private static final ContextTracer TRACER_DEBUG = new ContextTracer(OM.DEBUG, ObjyConnection.class);

  // private static final ContextTracer TRACER_INFO = new ContextTracer(OM.INFO, ObjyConnection.class);

  // TODO - session pools could be a configuration candidate.
  private static final String SESSION_POOL_NAME_READ = "ReadSP";

  private static final String SESSION_POOL_NAME_WRITE = "WriteSP";

  // private static final String PoolInfo = "PoolInfo";

  protected ConcurrentHashMap<String, ObjySession> readPool;

  protected ConcurrentHashMap<String, ObjySession> writePool;

  private ObjyPlacementManager defaultPlacementManager = null;

  private Object syncObject = new Object();

  private ReentrantLock lock = new ReentrantLock();

  private int sessionMinCacheSize = 600;

  private int sessionMaxCacheSize = 1000;

  private final int minInactiveSessions = 5;

  private int sessionCount = 0;

  private String logDirPath = null;

  private int logOption = oo.LogNone;

  public ObjyConnection()
  {
    readPool = new ConcurrentHashMap<String, ObjySession>(5);
    writePool = new ConcurrentHashMap<String, ObjySession>(5);
  }

  /***
   * Connect to a store and an FD. TODO - We might need to allow switching of FD in the future.
   */
  synchronized public void connect(IObjectivityStoreConfig storeConfig)
  {
    /****
     * If
     */
    fdName = storeConfig.getFdName();
    logDirPath = storeConfig.getLogPath();
    logOption = storeConfig.getLogOption();
    connect();
    // this.store = store;
  }

  synchronized public void connect(String fdName, int logOption)
  {
    /****
     * If
     */
    this.fdName = fdName;
    this.logOption = logOption;
    connect();
    // this.store = store;
  }

  private void connect()
  {

    if (TRACER_DEBUG.isEnabled())
    {
      TRACER_DEBUG.trace(" SessionMinCacheSize: " + sessionMinCacheSize);
      TRACER_DEBUG.trace(" SessionMaxCacheSize: " + sessionMaxCacheSize);
    }

    if (!isConnected)
    {
      try
      {
        if (Connection.current() == null)
        {
          if (logOption != oo.LogNone)
          {
            Connection.setLoggingOptions(logOption, true, // boolean logToFiles
                true, // boolean appendLogFiles,
                logDirPath, // String logDirPath,
                "MainLog.txt"// String mainLogFileName
                );
          }
          if (TRACER_DEBUG.isEnabled())
          {
            TRACER_DEBUG.trace(" creating new Connection");
          }
          connection = Connection.open(fdName, oo.openReadWrite);
          connection.useContextClassLoader(true);
        }
        else
        {
          connection.addToMainLog("ObjyConnection.connect()", "...reopen connection to the FD.");
          connection.setOpenMode(oo.openReadWrite);
          connection.reopen();
          connection.loadSchemaClasses(true);
        }
        isConnected = true;
      }
      catch (DatabaseOpenException e)
      {
        e.printStackTrace();
      }
      catch (DatabaseNotFoundException e)
      {
        e.printStackTrace();
      }
    }
  }

  public String getSessionPoolNameRead()
  {
    return SESSION_POOL_NAME_READ;
  }

  public String getSessionPoolNameWrite()
  {
    return SESSION_POOL_NAME_WRITE;
  }

  public ObjySession getWriteSessionFromPool(String sessionName)
  {
    return getSessionFromPool(sessionName);
  }

  public ObjySession getReadSessionFromPool(String sessionName)
  {
    return getSessionFromPool(sessionName);
  }

  protected ObjySession getSessionFromPool(String sessionName)
  {
    synchronized (syncObject)
    {
      // return connection.getSessionFromPool(getSessionPoolNameWrite(), sessionName);
      ObjySession session = readPool.get(sessionName);
      if (session == null)
      {
        if (sessionCount >= minInactiveSessions)
        {
          // look for an inactive one, rename it and use it.
          for (ObjySession objySession : readPool.values())
          {
            if (objySession.isAvailable())
            {
              objySession.setName(sessionName);
              session = objySession;
              break;
            }
          }
        }

        // we are forced to create one.
        if (session == null)
        {
          session = new ObjySession(sessionName, readPool, this);
          ++sessionCount;
          // System.out.println(">>> IS: creating new session: " + sessionName + " - total: " + sessionCount);
          readPool.put(sessionName, session);
        }
      }
      session.join();
      session.setAvailable(false);
      return session;
    }
  }

  public void disconnect()
  {
    if (!isConnected)
    {
      return;
    }
    // synchronized(syncObject)
    {
      // it's important to do the lock() call, otherwise during the test-suite
      // run we can exit the test before cleaning up, and session might be
      // partly terminated.
      // We could change the code in cleanupSessionPool() to remove the session
      // from the pool before terminating it, but this could leave some sessions
      // in the connection (another issue here is the connection.reconnect()
      // doesn't work all the time).
      lock.lock();
      if (TRACER_DEBUG.isEnabled())
      {
        TRACER_DEBUG.trace("ObjyConnection.disconnect() -- Start. " + toString());
      }

      // terminate the session and cleanup the Pool.
      // TRACER_DEBUG.trace("ObjyConnection.disconnect() -- cleanup readPool. ");
      cleanupSessionPool(readPool);
      // TRACER_DEBUG.trace("ObjyConnection.disconnect() -- cleanup writePool. ");
      cleanupSessionPool(writePool);

      sessionCount = 0;

      // TRACER_DEBUG.trace("ObjyConnection.disconnect() -- cleanup any other sessions. ");
      // for testing we need to find out if there are any open sessions.

      @SuppressWarnings("unchecked")
      Vector<Session> sessions = connection.sessions();
      for (Session aSession : sessions)
      {
        if (TRACER_DEBUG.isEnabled())
        {
          TRACER_DEBUG.trace("Session: " + aSession + " - open state: " + aSession.isOpen());
        }
        // we need to make sure that any open session is aborted, otherwise we
        // can't reopen the fd.
        if (aSession.isOpen())
        {
          try
          {
            aSession.join();
            aSession.abort();
            // IS: sometime we get exception about no transaction, although we checked
            // aSession.isOpen() above.
          }
          catch (ObjyRuntimeException ex)
          {
            ex.printStackTrace();
          }
          finally
          {
            aSession.terminate();
          }
        }
      }

      // 100211:IS - Avoid closing the connection, we're seeing
      // sort of schema issues doing so with 9.4.1...
      /****
       * try { Session session = new Session(); session.begin(); //connection.dropAllUserClasses(true);
       * connection.dropAllUnregisterableClasses(); session.commit(); connection.close(); isConnected = false; } catch
       * (DatabaseClosedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
       ****/
      if (TRACER_DEBUG.isEnabled())
      {
        TRACER_DEBUG.trace("ObjyConnection.disconnect() -- END. ");
      }
      lock.unlock();
    }

  }

  // public void resetFD()
  // {
  // //fdManager.resetFD();
  // if (Connection.current() != null)
  // {
  // if (!isConnected)
  // connect();
  //
  // // for testing we need to find out if there are any open sessions.
  // Vector<Session> sessions = Connection.current().sessions();
  // System.out.println("Sessions still available: " + sessions.size());
  // for (Session aSession : sessions)
  // {
  // System.out.println("Session: " + aSession + " - open state: " + aSession.isOpen());
  // // we need to make sure that any open session is aborted, otherwise we
  // // can't reopen the fd.
  // if (aSession.isOpen())
  // {
  // try {
  // aSession.join();
  // aSession.abort();
  // // IS: sometime we get exception about no transaction, although we checked
  // // aSession.isOpen() above.
  // } catch (ObjyRuntimeException ex) {
  // ex.printStackTrace();
  // } finally {
  // aSession.terminate();
  // }
  // }
  // }
  //
  // // Session session = new Session();
  // // session.begin();
  // // Iterator itr = session.getFD().containedDBs();
  // // ooDBObj dbObj = null;
  // // List<ooDBObj> dbList = new ArrayList<ooDBObj>();
  // // while (itr.hasNext())
  // // {
  // // dbObj = (ooDBObj) itr.next();
  // // dbList.add(dbObj);
  // // }
  // // itr.close();
  // // session.commit();
  //
  // // session.begin();
  // // for (ooDBObj db : dbList)
  // // {
  // // System.out.println("restFD() - deleting DB(" + db.getOid().getStoreString()+"):" + db.getName());
  // // db.delete();
  // // }
  // // session.commit();
  //
  // // we need to wipe the schema, some tests have similar class and package
  // // names which could cause tests to fail.
  // // for now we'll just wipe the wole FD.
  // //fdManager.resetFD_OLD();
  //
  // //
  // // System.out.println("resetFD() - dumping catalog BEGIN.........");
  // // session.begin();
  // // session.getFD().dumpCatalog();
  // // session.commit();
  // // System.out.println("resetFD() - dumping catalog END...........");
  // // session.terminate();
  //
  // disconnect();
  // }
  // }

  public void registerClass(String name)
  {
    connection.registerClass(name);
  }

  public ObjyPlacementManager getDefaultPlacementManager()
  {
    if (defaultPlacementManager == null)
    {
      defaultPlacementManager = new ObjyPlacementManagerImpl();
    }
    return defaultPlacementManager;
  }

  protected void cleanupSessionPool(ConcurrentHashMap<String, ObjySession> pool)
  {
    for (ObjySession objySession : pool.values())
    {
      try
      {
        if (objySession.isOpen())
        {
          objySession.join();
          objySession.abort();
          // IS: sometime we get exception about no transaction, although we checked
          // aSession.isOpen() above.
        }
      }
      catch (ObjyRuntimeException ex)
      {
        ex.printStackTrace();
      }
      finally
      {
        // TRACER_DEBUG.trace("ObjyConnection.cleanupSessionPool() -- start terminating session. " +
        // objySession.toString());
        try
        {
          objySession.terminate();
          // TRACER_DEBUG.trace("ObjyConnection.cleanupSessionPool() -- end terminating session. " +
          // objySession.toString());
        }
        catch (ObjyRuntimeException ex)
        {
          ex.printStackTrace();
        }
      }
    }
    pool.clear();
  }

  public void setSessionMinCacheSize(int sessionMinCacheSize)
  {
    if (sessionMinCacheSize > this.sessionMinCacheSize)
    {
      this.sessionMinCacheSize = sessionMinCacheSize;
    }
  }

  public void setSessionMaxCacheSize(int sessionMaxCacheSize)
  {
    if (sessionMaxCacheSize > this.sessionMaxCacheSize)
    {
      this.sessionMaxCacheSize = sessionMaxCacheSize;
    }
  }

  public int getMinSessionCacheSize()
  {
    return sessionMinCacheSize;
  }

  public int getMaxSessionCacheSize()
  {
    return sessionMaxCacheSize;
  }
}

Back to the top