synchronized IbisIdentifier getElectionResult(String election, long timeout) throws IOException { long deadline = System.currentTimeMillis() + timeout; if (timeout == 0) { deadline = Long.MAX_VALUE; } Election result = elections.get(election); while (result == null) { long timeRemaining = deadline - System.currentTimeMillis(); if (timeRemaining <= 0) { logger.debug("getElectionResullt deadline expired"); return null; } try { if (timeRemaining > 1000) { timeRemaining = 1000; } logger.debug("waiting " + timeRemaining + " for election"); wait(timeRemaining); logger.debug("DONE waiting " + timeRemaining + " for election"); } catch (final InterruptedException e) { // IGNORE } result = elections.get(election); } logger.debug("getElection result = " + result); return result.getWinner(); }
void writeState(DataOutputStream out, int joinTime) throws IOException { ByteArrayOutputStream arrayOut = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(arrayOut); synchronized (this) { if (!initialized) { throw new IOException("state not initialized yet"); } dataOut.writeInt(time); members.writeTo(dataOut); elections.writeTo(dataOut); Event[] signals = eventList.getSignalEvents(joinTime, time); dataOut.writeInt(signals.length); for (Event event : signals) { event.writeTo(dataOut); } dataOut.writeBoolean(closed); if (closed) { closeEvent.writeTo(out); } dataOut.writeBoolean(terminated); if (terminated) { terminateEvent.writeTo(out); } } dataOut.flush(); dataOut.close(); byte[] bytes = arrayOut.toByteArray(); out.writeInt(bytes.length); out.write(bytes); logger.debug("pool state size = " + bytes.length); }
private synchronized void handleEvent(Event event) { logger.debug("handling event: " + event); switch (event.getType()) { case Event.JOIN: members.add(new Member(event.getIbis(), event)); if (statistics != null) { statistics.newPoolSize(members.size()); } break; case Event.LEAVE: members.remove(event.getIbis()); if (statistics != null) { statistics.newPoolSize(members.size()); } break; case Event.DIED: IbisIdentifier died = event.getIbis(); members.remove(died); if (statistics != null) { statistics.newPoolSize(members.size()); } if (died.equals(registry.getIbisIdentifier())) { logger.debug("we were declared dead"); stop(); } break; case Event.SIGNAL: // Not handled here break; case Event.ELECT: elections.put(new Election(event)); if (statistics != null) { statistics.electionEvent(); } break; case Event.UN_ELECT: elections.remove(event.getDescription()); if (statistics != null) { statistics.electionEvent(); } break; case Event.POOL_CLOSED: closed = true; closeEvent = event; break; case Event.POOL_TERMINATED: terminated = true; terminateEvent = event; break; default: logger.error("unknown event type: " + event.getType()); } // wake up threads waiting for events notifyAll(); if (logger.isDebugEnabled()) { logger.debug("member list now: " + members); } }
void init(DataInputStream stream) throws IOException { long start = System.currentTimeMillis(); // copy over data first so we are not blocked while reading data byte[] bytes = new byte[stream.readInt()]; stream.readFully(bytes); DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes)); long read = System.currentTimeMillis(); // we have all the data in the array now, read from that... synchronized (this) { long locked = System.currentTimeMillis(); if (initialized) { // already initialized, ignore return; } logger.debug("reading bootstrap state"); time = in.readInt(); members.init(in); long membersDone = System.currentTimeMillis(); elections.init(in); int nrOfSignals = in.readInt(); if (nrOfSignals < 0) { throw new IOException("negative number of signals"); } ArrayList<Event> signals = new ArrayList<Event>(); for (int i = 0; i < nrOfSignals; i++) { signals.add(new Event(in)); } closed = in.readBoolean(); if (closed) { closeEvent = new Event(in); } terminated = in.readBoolean(); if (terminated) { terminateEvent = new Event(in); } // Create list of "old" events SortedSet<Event> events = new TreeSet<Event>(); events.addAll(members.getJoinEvents()); events.addAll(elections.getEvents()); events.addAll(signals); if (closed) { events.add(closeEvent); } if (terminated) { events.add(terminateEvent); } long used = System.currentTimeMillis(); // pass old events to the registry // CALLS REGISTRY WHILE POOL IS LOCKED! for (Event event : events) { registry.handleEvent(event); } long handled = System.currentTimeMillis(); initialized = true; notifyAll(); if (statistics != null) { statistics.newPoolSize(members.size()); } long statted = System.currentTimeMillis(); logger.info( "pool init, read = " + (read - start) + ", locked = " + (locked - read) + ", membersDone = " + (membersDone - locked) + ", used = " + (used - membersDone) + ", handled = " + (handled - used) + ", statted = " + (statted - handled)); } handleEvents(); logger.debug("bootstrap complete"); }