@Test public void testStatesGetDisabled() { SentryStateBank.enableState(TestState.COMPONENT, TestState.FIRST_STATE); assertTrue("Expected FIRST_STATE to be enabled", SentryStateBank.isEnabled(TestState.COMPONENT, TestState.FIRST_STATE)); SentryStateBank.disableState(TestState.COMPONENT, TestState.FIRST_STATE); assertFalse("Expected FIRST_STATE to be disabled", SentryStateBank.isEnabled(TestState.COMPONENT, TestState.FIRST_STATE)); }
public static void clearAllStates() { SentryStateBank.clearAllStates(); }
@Test public void testCheckMultipleStateCheckSuccess() { SentryStateBank.enableState(TestState.COMPONENT, TestState.FIRST_STATE); SentryStateBank.enableState(TestState.COMPONENT, TestState.SECOND_STATE); assertTrue("Expected both FIRST_STATE and SECOND_STATE to be enabled", SentryStateBank.hasStatesEnabled(TestState.COMPONENT, new HashSet<SentryState>( Arrays.asList(TestState.FIRST_STATE, TestState.SECOND_STATE)))); }
/** * Retrieve a Hive full snapshot from HMS. * * @return HMS snapshot. Snapshot consists of a mapping from auth object name to the set of paths * corresponding to that name. */ private Map<String, Collection<String>> fetchFullUpdate() throws Exception{ String logMessage = "Request full HMS snapshot"; LOGGER.info(logMessage); System.out.println(SentryServiceUtil.getCurrentTimeStampWithMessage(logMessage)); try (FullUpdateInitializer updateInitializer = new FullUpdateInitializer(hiveConnectionFactory, conf); Context context = updateTimer.time()) { SentryStateBank.enableState(FullUpdateInitializerState.COMPONENT,FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS); Map<String, Collection<String>> pathsUpdate = updateInitializer.getFullHMSSnapshot(); logMessage = "Obtained full HMS snapshot"; LOGGER.info(logMessage); System.out.println(SentryServiceUtil.getCurrentTimeStampWithMessage(logMessage)); return pathsUpdate; } catch (Exception exception) { failedSnapshotsCount.inc(); LOGGER.error("Snapshot created failed ", exception); throw exception; } finally { SentryStateBank.disableState(FullUpdateInitializerState.COMPONENT,FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS); } } }
/** * Creating snapshot when SentryHMSClient is not connected to HMS */ @Test public void testSnapshotCreationWithOutClientConnected() throws Exception { // Make sure that client is not connected Assert.assertFalse(client.isConnected()); PathsImage snapshotInfo = client.getFullSnapshot(); Assert.assertTrue(snapshotInfo.getPathImage().isEmpty()); Assert.assertFalse("FullUpdateInitializer is not expected to be in progress", SentryStateBank.isEnabled(FullUpdateInitializerState.COMPONENT, FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)); Assert.assertFalse(SentryStateBank.wereStatesEnabled(FullUpdateInitializerState.COMPONENT, new HashSet<SentryState>( Arrays.asList(FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)))); }
@Test public void testEnableState() { SentryStateBank.enableState(TestState.COMPONENT, TestState.FIRST_STATE); assertTrue("Expected FIRST_STATE to be enabled", SentryStateBank.isEnabled(TestState.COMPONENT, TestState.FIRST_STATE)); assertFalse("Expected SECOND_STATE to be disabled", SentryStateBank.isEnabled(TestState.COMPONENT, TestState.SECOND_STATE)); }
SentryStateBank.enableState(SentryServiceState.COMPONENT, SentryServiceState.SERVICE_RUNNING); thriftServer.serve();
@Override public void close() { if (client != null) { // Close any outstanding connections to HMS try { client.disconnect(); SentryStateBank.disableState(HMSFollowerState.COMPONENT,HMSFollowerState.CONNECTED); } catch (Exception failure) { LOGGER.error("Failed to close the Sentry Hms Client", failure); } } notificationFetcher.close(); }
/** * Syncronizes with the eventId processed by sentry * @param eventId * @return current counter value that should be no smaller then the requested * value, returns 0 if there were an exception. */ long syncEventId(long eventId) { try { if (!SentryStateBank.isEnabled(FullUpdateInitializerState.COMPONENT, FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)) { return sentryStore.getCounterWait().waitFor(eventId); } else { LOGGER.info("HMS event synchronization is disabled temporarily as sentry is in the process of " + "fetching full snapshot. No action needed"); return eventId; } } catch (InterruptedException e) { String msg = String.format("wait request for id %d is interrupted", eventId); LOGGER.error(msg, e); Thread.currentThread().interrupt(); } catch (TimeoutException e) { String msg = String.format("timed out wait request for id %d", eventId); LOGGER.warn(msg, e); } return 0; } }
public static void resetComponentState(String component) { SentryStateBank.resetComponentState(component); } }
/** * Creating snapshot when HMS doesn't have any data */ @Test public void testSnapshotCreationWithNoHmsData() throws Exception { MockClient mockClient = new MockClient(new HiveSnapshot(), 1); client.setClient(mockClient.client); // Make sure that client is connected Assert.assertTrue(client.isConnected()); PathsImage snapshotInfo = client.getFullSnapshot(); Assert.assertTrue(snapshotInfo.getPathImage().isEmpty()); Assert.assertFalse("FullUpdateInitializer is not expected to be in progress", SentryStateBank.isEnabled(FullUpdateInitializerState.COMPONENT, FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)); Assert.assertTrue(SentryStateBank.wereStatesEnabled(FullUpdateInitializerState.COMPONENT, new HashSet<SentryState>( Arrays.asList(FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)))); }
@Override public void run() { SentryStateBank.enableState(HMSFollowerState.COMPONENT,HMSFollowerState.STARTED); long lastProcessedNotificationId; try { try { // Initializing lastProcessedNotificationId based on the latest persisted notification ID. lastProcessedNotificationId = sentryStore.getLastProcessedNotificationID(); } catch (Exception e) { LOGGER.error("Failed to get the last processed notification id from sentry store, " + "Skipping the processing", e); return; } // Wake any clients connected to this service waiting for HMS already processed notifications. wakeUpWaitingClientsForSync(lastProcessedNotificationId); // Only the leader should listen to HMS updates if (!isLeader()) { // Close any outstanding connections to HMS close(); return; } syncupWithHms(lastProcessedNotificationId); } finally { SentryStateBank.disableState(HMSFollowerState.COMPONENT,HMSFollowerState.STARTED); } }
client.connect(); connectedToHms = true; SentryStateBank.enableState(HMSFollowerState.COMPONENT,HMSFollowerState.CONNECTED); } catch (Throwable e) { LOGGER.error("HMSFollower cannot connect to HMS!!", e);
exception.ifExceptionThrow(); SentryStateBank.disableState(SentryServiceState.COMPONENT,SentryServiceState.SERVICE_RUNNING); LOGGER.info("Stopped...");
Preconditions.checkState(!SentryStateBank.isEnabled(SentryServiceState.COMPONENT, SentryServiceState.FULL_UPDATE_RUNNING), "HMSFollower shown loading full snapshot when it should not be."); .enableState(SentryServiceState.COMPONENT, SentryServiceState.FULL_UPDATE_RUNNING); } finally { SentryStateBank .disableState(SentryServiceState.COMPONENT, SentryServiceState.FULL_UPDATE_RUNNING);
Assert.assertEquals(snapshotInfo.getId(), mockClient.eventId); Assert.assertFalse("FullUpdateInitializer is not expected to be in progress", SentryStateBank.isEnabled(FullUpdateInitializerState.COMPONENT, FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)); Assert.assertTrue(SentryStateBank.wereStatesEnabled(FullUpdateInitializerState.COMPONENT, new HashSet<SentryState>( Arrays.asList(FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS))));
@Test public void testCheckMultipleStateCheckFailure() { SentryStateBank.enableState(TestState.COMPONENT, TestState.FIRST_STATE); assertFalse("Expected only FIRST_STATE to be enabled", SentryStateBank.hasStatesEnabled(TestState.COMPONENT, new HashSet<SentryState>( Arrays.asList(TestState.FIRST_STATE, TestState.SECOND_STATE)))); }
SentryStateBank.enableState(FullUpdateInitializerState.COMPONENT, FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS);
@Before public void setUp() { SentryStateBank.clearAllStates(); }
Assert.assertTrue(snapshotInfo.getPathImage().isEmpty()); Assert.assertFalse("FullUpdateInitializer is not expected to be in progress", SentryStateBank.isEnabled(FullUpdateInitializerState.COMPONENT, FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS)); Assert.assertTrue(SentryStateBank.wereStatesEnabled(FullUpdateInitializerState.COMPONENT, new HashSet<SentryState>( Arrays.asList(FullUpdateInitializerState.FULL_SNAPSHOT_INPROGRESS))));