/** * Convert any exception into a {@link RuntimeException}. * All other exception types are wrapped in a new instance of * {@code ServiceStateException}. * @param fault exception or throwable * @return a {@link RuntimeException} to rethrow */ public static RuntimeException convert(Throwable fault) { if (fault instanceof RuntimeException) { return (RuntimeException) fault; } else { return new ServiceStateException(fault); } }
public void init(Configuration conf) { if (conf == null) { throw new ServiceStateException("Cannot initialize service " + getName() + ": null configuration"); noteFailure(e); ServiceOperations.stopQuietly(LOG, this); throw ServiceStateException.convert(e);
throw ServiceStateException.convert(firstException);
/** * Test that active elector service triggers a fatal RM Event when connection * to ZK fails. YARN-8409 */ @Test public void testFailureToConnectToZookeeper() throws Exception { stopServer(); Configuration myConf = new Configuration(conf); ResourceManager rm = new MockRM(conf); ActiveStandbyElectorBasedElectorService ees = new ActiveStandbyElectorBasedElectorService(rm); try { ees.init(myConf); Assert.fail("expect failure to connect to Zookeeper"); } catch (ServiceStateException sse) { Assert.assertTrue(sse.getMessage().contains("ConnectionLoss")); } }
@Test public void testNoAuthExceptionInNonHAMode() throws Exception { TestZKRMStateStoreTester zkTester = new TestZKRMStateStoreTester(); String appRoot = zkTester.getWorkingZNode() + "/ZKRMStateRoot/RMAppRoot" ; ZooKeeper zk = spy(createClient()); doThrow(new KeeperException.NoAuthException()).when(zk). create(appRoot, null, RMZKUtils.getZKAcls(new Configuration()), CreateMode.PERSISTENT); try { zkTester.getRMStateStore(zk); fail("Expected exception to be thrown"); } catch(ServiceStateException e) { assertNotNull(e.getCause()); assertTrue("Expected NoAuthException", e.getCause() instanceof KeeperException.NoAuthException); } }
throw ServiceStateException.convert(e); } finally {
@Test public void testCheckVersion() throws IOException { LeveldbTimelineStore dbStore = (LeveldbTimelineStore) store; // default version Version defaultVersion = dbStore.getCurrentVersion(); Assert.assertEquals(defaultVersion, dbStore.loadVersion()); // compatible version Version compatibleVersion = Version.newInstance(defaultVersion.getMajorVersion(), defaultVersion.getMinorVersion() + 2); dbStore.storeVersion(compatibleVersion); Assert.assertEquals(compatibleVersion, dbStore.loadVersion()); restartTimelineStore(); dbStore = (LeveldbTimelineStore) store; // overwrite the compatible version Assert.assertEquals(defaultVersion, dbStore.loadVersion()); // incompatible version Version incompatibleVersion = Version.newInstance( defaultVersion.getMajorVersion() + 1, defaultVersion.getMinorVersion()); dbStore.storeVersion(incompatibleVersion); try { restartTimelineStore(); Assert.fail("Incompatible version, should expect fail here."); } catch (ServiceStateException e) { Assert.assertTrue("Exception message mismatch", e.getMessage().contains("Incompatible version for timeline store")); } }
/** * Verify that that a service is in a given state. * @param expectedState the desired state * @throws ServiceStateException if the service state is different from * the desired state */ public void ensureCurrentState(Service.STATE expectedState) { if (state != expectedState) { throw new ServiceStateException(name+ ": for this operation, the " + "current service state must be " + expectedState + " instead of " + state); } }
public void init(Configuration conf) { if (conf == null) { throw new ServiceStateException("Cannot initialize service " + getName() + ": null configuration"); noteFailure(e); ServiceOperations.stopQuietly(LOG, this); throw ServiceStateException.convert(e);
/** * {@inheritDoc} * @throws ServiceStateException if the current service state does not permit * this action */ @Override public void start() { if (isInState(STATE.STARTED)) { return; } //enter the started state synchronized (stateChangeLock) { if (stateModel.enterState(STATE.STARTED) != STATE.STARTED) { try { startTime = System.currentTimeMillis(); serviceStart(); if (isInState(STATE.STARTED)) { //if the service started (and isn't now in a later state), notify LOG.debug("Service {} is started", getName()); notifyListeners(); } } catch (Exception e) { noteFailure(e); ServiceOperations.stopQuietly(LOG, this); throw ServiceStateException.convert(e); } } } }
@Test public void testCheckVersion() throws IOException { RollingLevelDBTimelineStore dbStore = (RollingLevelDBTimelineStore) store; // default version Version defaultVersion = dbStore.getCurrentVersion(); Assert.assertEquals(defaultVersion, dbStore.loadVersion()); // compatible version Version compatibleVersion = Version.newInstance(defaultVersion.getMajorVersion(), defaultVersion.getMinorVersion() + 2); dbStore.storeVersion(compatibleVersion); Assert.assertEquals(compatibleVersion, dbStore.loadVersion()); restartTimelineStore(); dbStore = (RollingLevelDBTimelineStore) store; // overwrite the compatible version Assert.assertEquals(defaultVersion, dbStore.loadVersion()); // incompatible version Version incompatibleVersion = Version.newInstance(defaultVersion.getMajorVersion() + 1, defaultVersion.getMinorVersion()); dbStore.storeVersion(incompatibleVersion); try { restartTimelineStore(); Assert.fail("Incompatible version, should expect fail here."); } catch (ServiceStateException e) { Assert.assertTrue("Exception message mismatch", e.getMessage().contains("Incompatible version for timeline store")); } }
/** * Convert any exception into a {@link RuntimeException}. * If the caught exception is already of that type, it is typecast to a * {@link RuntimeException} and returned. * * All other exception types are wrapped in a new instance of * {@code ServiceStateException}. * @param text text to use if a new exception is created * @param fault exception or throwable * @return a {@link RuntimeException} to rethrow */ public static RuntimeException convert(String text, Throwable fault) { if (fault instanceof RuntimeException) { return (RuntimeException) fault; } else { return new ServiceStateException(text, fault); } } }
public void init(Configuration conf) { if (conf == null) { throw new ServiceStateException("Cannot initialize service " + getName() + ": null configuration"); noteFailure(e); ServiceOperations.stopQuietly(LOG, this); throw ServiceStateException.convert(e);
void stopServices() { // stop in reverse order of start List<Service> serviceList = new ArrayList<Service>(services.size()); for (ServiceWithDependency sd : services.values()) { serviceList.add(sd.service); } Exception firstException = null; for (int i = services.size() - 1; i >= 0; i--) { Service service = serviceList.get(i); if (LOG.isDebugEnabled()) { LOG.debug("Stopping service : " + service); } Exception ex = ServiceOperations.stopQuietly(service); if (ex != null && firstException == null) { LOG.warn("Failed to stop service, name=" + service.getName(), ex); firstException = ex; } } //after stopping all services, rethrow the first exception raised if (firstException != null) { throw ServiceStateException.convert(firstException); } }
} catch (ServiceStateException e) { Assert.assertTrue("Exception message mismatch", e.getMessage().contains("Incompatible version for timeline state store"));
/** * Check that a state tansition is valid and * throw an exception if not * @param name name of the service (can be null) * @param state current state * @param proposed proposed new state */ public static void checkStateTransition(String name, Service.STATE state, Service.STATE proposed) { if (!isValidStateTransition(state, proposed)) { throw new ServiceStateException(name + " cannot enter state " + proposed + " from state " + state); } }
public void init(Configuration conf) { if (conf == null) { throw new ServiceStateException("Cannot initialize service " + getName() + ": null configuration"); noteFailure(e); ServiceOperations.stopQuietly(LOG, this); throw ServiceStateException.convert(e);
throw ServiceStateException.convert(firstError);
/** * Verify that that a service is in a given state. * @param expectedState the desired state * @throws ServiceStateException if the service state is different from * the desired state */ public void ensureCurrentState(Service.STATE expectedState) { if (state != expectedState) { throw new ServiceStateException(name+ ": for this operation, the " + "current service state must be " + expectedState + " instead of " + state); } }
public void init(Configuration conf) { if (conf == null) { throw new ServiceStateException("Cannot initialize service " + getName() + ": null configuration"); noteFailure(e); ServiceOperations.stopQuietly(LOG, this); throw ServiceStateException.convert(e);