use of org.apache.hadoop.hive.metastore.api.NotificationEventRequest in project hive by apache.
the class TestObjectStore method testNotificationOps.
/**
* Test notification operations
*/
// TODO MS-SPLIT uncomment once we move EventMessage over
@Test
public void testNotificationOps() throws InterruptedException {
final int NO_EVENT_ID = 0;
final int FIRST_EVENT_ID = 1;
final int SECOND_EVENT_ID = 2;
NotificationEvent event = new NotificationEvent(0, 0, EventMessage.EventType.CREATE_DATABASE.toString(), "");
NotificationEventResponse eventResponse;
CurrentNotificationEventId eventId;
// Verify that there is no notifications available yet
eventId = objectStore.getCurrentNotificationEventId();
Assert.assertEquals(NO_EVENT_ID, eventId.getEventId());
// Verify that addNotificationEvent() updates the NotificationEvent with the new event ID
objectStore.addNotificationEvent(event);
Assert.assertEquals(FIRST_EVENT_ID, event.getEventId());
objectStore.addNotificationEvent(event);
Assert.assertEquals(SECOND_EVENT_ID, event.getEventId());
// Verify that objectStore fetches the latest notification event ID
eventId = objectStore.getCurrentNotificationEventId();
Assert.assertEquals(SECOND_EVENT_ID, eventId.getEventId());
// Verify that getNextNotification() returns all events
eventResponse = objectStore.getNextNotification(new NotificationEventRequest());
Assert.assertEquals(2, eventResponse.getEventsSize());
Assert.assertEquals(FIRST_EVENT_ID, eventResponse.getEvents().get(0).getEventId());
Assert.assertEquals(SECOND_EVENT_ID, eventResponse.getEvents().get(1).getEventId());
// Verify that getNextNotification(last) returns events after a specified event
eventResponse = objectStore.getNextNotification(new NotificationEventRequest(FIRST_EVENT_ID));
Assert.assertEquals(1, eventResponse.getEventsSize());
Assert.assertEquals(SECOND_EVENT_ID, eventResponse.getEvents().get(0).getEventId());
// Verify that getNextNotification(last) returns zero events if there are no more notifications available
eventResponse = objectStore.getNextNotification(new NotificationEventRequest(SECOND_EVENT_ID));
Assert.assertEquals(0, eventResponse.getEventsSize());
// Verify that cleanNotificationEvents() cleans up all old notifications
Thread.sleep(1);
objectStore.cleanNotificationEvents(1);
eventResponse = objectStore.getNextNotification(new NotificationEventRequest());
Assert.assertEquals(0, eventResponse.getEventsSize());
}
use of org.apache.hadoop.hive.metastore.api.NotificationEventRequest in project hive by apache.
the class TestObjectStore method testConcurrentAddNotifications.
@Ignore("This test is here to allow testing with other databases like mysql / postgres etc\n" + " with user changes to the code. This cannot be run on apache derby because of\n" + " https://db.apache.org/derby/docs/10.10/devguide/cdevconcepts842385.html")
@Test
public void testConcurrentAddNotifications() throws ExecutionException, InterruptedException {
final int NUM_THREADS = 10;
CyclicBarrier cyclicBarrier = new CyclicBarrier(NUM_THREADS, () -> LoggerFactory.getLogger("test").debug(NUM_THREADS + " threads going to add notification"));
Configuration conf = MetastoreConf.newMetastoreConf();
MetaStoreTestUtils.setConfForStandloneMode(conf);
/*
Below are the properties that need to be set based on what database this test is going to be run
*/
// conf.setVar(HiveConf.ConfVars.METASTORE_CONNECTION_DRIVER, "com.mysql.jdbc.Driver");
// conf.setVar(HiveConf.ConfVars.METASTORECONNECTURLKEY,
// "jdbc:mysql://localhost:3306/metastore_db");
// conf.setVar(HiveConf.ConfVars.METASTORE_CONNECTION_USER_NAME, "");
// conf.setVar(HiveConf.ConfVars.METASTOREPWD, "");
/*
we have to add this one manually as for tests the db is initialized via the metastoreDiretSQL
and we don't run the schema creation sql that includes the an insert for notification_sequence
which can be locked. the entry in notification_sequence happens via notification_event insertion.
*/
objectStore.getPersistenceManager().newQuery(MNotificationLog.class, "eventType==''").execute();
objectStore.getPersistenceManager().newQuery(MNotificationNextId.class, "nextEventId==-1").execute();
objectStore.addNotificationEvent(new NotificationEvent(0, 0, EventMessage.EventType.CREATE_DATABASE.toString(), "CREATE DATABASE DB initial"));
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++) {
final int n = i;
executorService.execute(() -> {
ObjectStore store = new ObjectStore();
store.setConf(conf);
String eventType = EventMessage.EventType.CREATE_DATABASE.toString();
NotificationEvent dbEvent = new NotificationEvent(0, 0, eventType, "CREATE DATABASE DB" + n);
System.out.println("ADDING NOTIFICATION");
try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
throw new RuntimeException(e);
}
store.addNotificationEvent(dbEvent);
System.out.println("FINISH NOTIFICATION");
});
}
executorService.shutdown();
Assert.assertTrue(executorService.awaitTermination(15, TimeUnit.SECONDS));
// we have to setup this again as the underlying PMF keeps getting reinitialized with original
// reference closed
ObjectStore store = new ObjectStore();
store.setConf(conf);
NotificationEventResponse eventResponse = store.getNextNotification(new NotificationEventRequest());
Assert.assertEquals(NUM_THREADS + 1, eventResponse.getEventsSize());
long previousId = 0;
for (NotificationEvent event : eventResponse.getEvents()) {
Assert.assertTrue("previous:" + previousId + " current:" + event.getEventId(), previousId < event.getEventId());
Assert.assertTrue(previousId + 1 == event.getEventId());
previousId = event.getEventId();
}
}
Aggregations