protected String getKey (String prefix, long id) { StringBuilder sb = new StringBuilder (getName()); sb.append ('.'); sb.append (prefix); sb.append (Long.toString (id)); return sb.toString (); } protected long initCounter (String name, long defValue) {
private void setThreadName (long id, String method, TransactionParticipant p) { Thread.currentThread().setName( String.format("%s:%d %s %s %s", getName(), id, method, p.getClass().getName(), LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())) ); } private void setThreadLocal (long id, Serializable context) {
protected void commit (int session, long id, Serializable context, List<TransactionParticipant> members, boolean recover, LogEvent evt, Profiler prof) { for (TransactionParticipant p :members) { if (recover && p instanceof ContextRecovery) { context = ((ContextRecovery) p).recover (id, context, true); if (evt != null) evt.addMessage (" commit-recover: " + getName(p)); } if (hasStatusListeners) notifyStatusListeners ( session, TransactionStatusEvent.State.COMMITING, id, getName(p), context ); commit (p, id, context); if (evt != null) { evt.addMessage (" commit: " + getName(p)); if (prof != null) prof.checkPoint (" commit: " + getName(p)); } } } protected void abort
protected void abort (int session, long id, Serializable context, List<TransactionParticipant> members, boolean recover, LogEvent evt, Profiler prof) { for (TransactionParticipant p :members) { if (recover && p instanceof ContextRecovery) { context = ((ContextRecovery) p).recover (id, context, false); if (evt != null) evt.addMessage (" abort-recover: " + getName(p)); } if (hasStatusListeners) notifyStatusListeners ( session, TransactionStatusEvent.State.ABORTING, id, getName(p), context ); abort(p, id, context); if (evt != null) { evt.addMessage (" abort: " + getName(p)); if (prof != null) prof.checkPoint (" abort: " + getName(p)); } } } protected int prepareForAbort
private Map<String,Object> txnmgrInfo(TransactionManager txnmgr) { Map<String,Object> m = new LinkedHashMap<>(); m.put ("name", txnmgr.getName()); m.put ("type", txnmgr.getClass().getSimpleName()); m.put ("tail", txnmgr.getTail()); m.put ("head", txnmgr.getHead()); m.put ("inTransit", txnmgr.getInTransit()); m.put ("TPSAvg", txnmgr.getTPSAvg()); m.put ("TPSPeak", txnmgr.getTPSPeak()); m.put ("TPSPeakWhen", txnmgr.getTPSPeakWhen()); m.put ("TPSElapsed", txnmgr.getTPSElapsed()); m.put ("metrics", txnmgr.getMetrics().metrics()); return m; }
@Override public void run() { Thread.currentThread().setName (getName()+"-retry-task"); while (running()) { for (Serializable context; (context = (Serializable)psp.rdp (RETRY_QUEUE)) != null;) { iisp.out (queue, context, retryTimeout); psp.inp (RETRY_QUEUE); } ISOUtil.sleep(retryInterval); } } }
private void initInternalRoutes() { routes.add(new Route<>(prefix + "/q2/version**", "GET", (t,s) -> mapOf ("version", q2Version()))); routes.add(new Route<>(prefix + "/q2/applicationVersion**", "GET", (t,s) -> mapOf("applicationVersion", Q2.getAppVersionString()))); routes.add(new Route<>(prefix + "/q2/instanceId**", "GET", (t,s) -> mapOf("instanceId", q2.getInstanceId()))); routes.add(new Route<>(prefix + "/q2/uptime**", "GET", (t,s) -> mapOf("uptime", q2.getUptime()))); routes.add(new Route<>(prefix + "/q2/started**", "GET", (t,s) -> mapOf("started", new Date(System.currentTimeMillis() - q2.getUptime())))); routes.add(new Route<>(prefix + "/q2/diskspace**", "GET", (t,s) -> diskspace())); routes.add(new Route<>(prefix + "/q2/mux/{muxname}**", "GET", (t,s) -> muxInfo(t,s))); routes.add(new Route<>(prefix + "/q2/mux**", "GET", (t,s) -> muxes())); routes.add(new Route<>(prefix + "/q2/txnmgr/name", "GET", (t,s) -> mapOf("name", txnmgr.getName()))); routes.add(new Route<>(prefix + "/q2/txnmgr**", "GET", (t,s) -> txnmgr())); }
@Override public void startService () throws Exception { NameRegistrar.register(getName(), this); recover(); threads = Collections.synchronizedList(new ArrayList(maxSessions));
@Override public void stopService () { NameRegistrar.unregister(getName()); if (loadMonitorExecutor != null) loadMonitorExecutor.shutdown(); Thread[] tt = threads.toArray(new Thread[threads.size()]); if (iisp != isp) for (Object o=iisp.inp(queue); o != null; o=iisp.inp(queue)) isp.out(queue, o); // push back to replicated space for (Thread t : tt) iisp.out(queue, Boolean.FALSE, 60 * 1000); for (Thread thread : tt) { try { thread.join (60*1000); threads.remove(thread); } catch (InterruptedException e) { getLog().warn ("Session " + thread.getName() +" does not respond - attempting to interrupt"); thread.interrupt(); } } tps.stop(); for (Destroyable destroyable : destroyables) { try { destroyable.destroy(); } catch (Throwable t) { getLog().warn (t); } } } public void queue (Serializable context) {
if (hasStatusListeners) notifyStatusListeners ( session, TransactionStatusEvent.State.PREPARING_FOR_ABORT, id, getName(p), context ); action = prepareForAbort (p, id, context); evt.addMessage("prepareForAbort: " + getName(p)); if (prof != null) prof.checkPoint ("prepareForAbort: " + getName(p)); session, TransactionStatusEvent.State.PREPARING, id, getName(p), context ); action = prepare (p, id, context); if (evt != null) { evt.addMessage (" prepare: " + getName(p) + (abort ? " ABORTED" : " PREPARED") + (retry ? " RETRY" : "") + ((action & NO_JOIN) == NO_JOIN ? " NO_JOIN" : "")); if (prof != null) prof.checkPoint ("prepare: " + getName(p)); snapshot (id, context); if (metrics != null) metrics.record(getName(p) + "-snapshot", c.elapsed()); } catch (Exception e) {
protected void commit (TransactionParticipant p, long id, Serializable context) { Chronometer c = new Chronometer(); try { setThreadName(id, "commit", p); p.commit(id, context); } catch (Throwable t) { getLog().warn ("COMMIT: " + Long.toString (id), t); } if (metrics != null) metrics.record(getName(p) + "-commit", c.elapsed()); } protected void abort
protected void abort (TransactionParticipant p, long id, Serializable context) { Chronometer c = new Chronometer(); try { setThreadName(id, "abort", p); p.abort(id, context); } catch (Throwable t) { getLog().warn ("ABORT: " + Long.toString (id), t); } if (metrics != null) metrics.record(getName(p) + "-abort", c.elapsed()); } protected int prepare
protected int prepare (TransactionParticipant p, long id, Serializable context) { Chronometer c = new Chronometer(); try { setThreadName(id, "prepare", p); return p.prepare (id, context); } catch (Throwable t) { getLog().warn ("PREPARE: " + Long.toString (id), t); } finally { if (metrics != null) metrics.record(getName(p) + "-prepare", c.elapsed()); } return ABORTED; } protected void commit
protected int prepareForAbort (TransactionParticipant p, long id, Serializable context) { Chronometer c = new Chronometer(); try { if (p instanceof AbortParticipant) { setThreadName(id, "prepareForAbort", p); return ((AbortParticipant)p).prepareForAbort (id, context); } } catch (Throwable t) { getLog().warn ("PREPARE-FOR-ABORT: " + Long.toString (id), t); } finally { if (metrics != null) metrics.record(getName(p) + "-prepare-for-abort", c.elapsed()); } return ABORTED | NO_JOIN; } protected int prepare
@Override public void run() { Thread.currentThread().setName (getName()+"-input-queue-monitor"); while (running()) { while (getOutstandingTransactions() > getActiveSessions() + threshold && running()) { ISOUtil.sleep(100L); } if (!running()) break; try { Object context = isp.in(queue, 1000L); if (context != null) { if (!running()) { isp.out(queue, context); // place it back break; } iisp.out(queue, context); } } catch (SpaceError e) { getLog().error(e); ISOUtil.sleep(1000L); // relax on error } } } }
paused = false; transactionActive = false; thread.setName (getName() + "-" + session + ":idle"); int action = -1; try { prof = pt.getProfiler(); if (metrics != null && prof != null) metrics.record(getName(pt.getParticipant()) + "-resume", prof.getPartialInMillis()); if (prof != null) prof.reenable();