Search in sources :

Example 11 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent in project ignite by apache.

the class IgfsEventsAbstractSelfTest method testFileDataEvents.

/**
 * Checks events on file data transfer operations.
 *
 * @throws Exception If failed.
 */
public void testFileDataEvents() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 5;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    final IgfsPath file = new IgfsPath("/file1");
    final int dataSize = 1024;
    byte[] buf = new byte[dataSize];
    // Will generate IGFS_FILE_CREATED, IGFS_FILE_OPENED_WRITE, IGFS_FILE_CLOSED_WRITE.
    try (IgfsOutputStream os = igfs.create(file, false)) {
        // Will generate no events.
        os.write(buf);
    }
    // Will generate EVT_IGFS_FILE_OPENED_READ, IGFS_FILE_CLOSED_READ.
    try (IgfsInputStream is = igfs.open(file, 256)) {
        // Will generate no events.
        is.readFully(0, buf);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    assertEquals((long) dataSize, evt.dataSize());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_OPENED_READ, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_CLOSED_READ, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    assertEquals((long) dataSize, evt.dataSize());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent in project ignite by apache.

the class IgfsEventsAbstractSelfTest method testTwoFiles.

/**
 * Checks events on CRUD operations on 2 files.
 *
 * @throws Exception If failed.
 */
public void testTwoFiles() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 4 + 3 + 2 + 2;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1");
    IgfsPath file1 = new IgfsPath(dir, "file1");
    // Will generate EVT_IGFS_FILE_CREATED event + EVT_IGFS_DIR_CREATED event + OPEN + CLOSE.
    igfs.create(file1, true).close();
    IgfsPath file2 = new IgfsPath(dir, "file2");
    // Will generate 1 EVT_IGFS_FILE_CREATED event + OPEN + CLOSE.
    igfs.create(file2, true).close();
    assertTrue(igfs.exists(dir));
    assertTrue(igfs.exists(file1));
    assertTrue(igfs.exists(file2));
    // Will generate 1 EVT_IGFS_FILE_DELETED and 1 EVT_IGFS_FILE_PURGED.
    assertTrue(igfs.delete(file1, false));
    // Same.
    assertTrue(igfs.delete(file2, false));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    assertEquals(0, evt.dataSize());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    evt = (IgfsEvent) evtList.get(5);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    evt = (IgfsEvent) evtList.get(6);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    assertEquals(0, evt.dataSize());
    assertOneToOne(evtList.subList(7, 11), new EventPredicate(EVT_IGFS_FILE_DELETED, new IgfsPath("/dir1/file1")), new EventPredicate(EVT_IGFS_FILE_PURGED, new IgfsPath("/dir1/file1")), new EventPredicate(EVT_IGFS_FILE_DELETED, new IgfsPath("/dir1/file2")), new EventPredicate(EVT_IGFS_FILE_PURGED, new IgfsPath("/dir1/file2")));
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent in project ignite by apache.

the class IgfsEventsAbstractSelfTest method testSingleFileNestedDirs.

/**
 * Checks events on CRUD operations on a single file in nested directories.
 *
 * @throws Exception If failed.
 */
public void testSingleFileNestedDirs() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 6 + 1 + 1;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1/dir2/dir3");
    IgfsPath file = new IgfsPath(dir, "file1");
    // Will generate 3 EVT_IGFS_DIR_CREATED + EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
    // EVT_IGFS_FILE_CLOSED and a number of EVT_IGFS_META_UPDATED.
    igfs.create(file, true).close();
    IgfsPath mvFile = new IgfsPath(dir, "mvFile1");
    // Will generate EVT_IGFS_FILE_RENAMED.
    igfs.rename(file, mvFile);
    // Will generate EVT_IGFS_DIR_DELETED event.
    assertTrue(igfs.delete(dir.parent(), true));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3"), evt.path());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    assertFalse(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(5);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    assertEquals(0, evt.dataSize());
    evt = (IgfsEvent) evtList.get(6);
    assertEquals(EVT_IGFS_FILE_RENAMED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/mvFile1"), evt.newPath());
    evt = (IgfsEvent) evtList.get(7);
    assertEquals(EVT_IGFS_DIR_DELETED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent in project ignite by apache.

the class IgfsUtils method sendEvents.

/**
 * Sends a series of event.
 *
 * @param kernalCtx Kernal context.
 * @param path The path of the created file.
 * @param newPath New path.
 * @param type The type of event to send.
 */
public static void sendEvents(GridKernalContext kernalCtx, IgfsPath path, IgfsPath newPath, int type) {
    assert kernalCtx != null;
    assert path != null;
    GridEventStorageManager evts = kernalCtx.event();
    ClusterNode locNode = kernalCtx.discovery().localNode();
    if (evts.isRecordable(type)) {
        if (newPath == null)
            evts.record(new IgfsEvent(path, locNode, type));
        else
            evts.record(new IgfsEvent(path, newPath, locNode, type));
    }
}
Also used : GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgfsEvent(org.apache.ignite.events.IgfsEvent)

Example 15 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent in project ignite by apache.

the class IgfsAbstractOutputStream method updateMetricsOnClose.

/**
 * Updates IGFS metrics when the stream is closed.
 */
protected void updateMetricsOnClose() {
    IgfsLocalMetrics metrics = igfsCtx.metrics();
    metrics.addWrittenBytesTime(bytes, time);
    metrics.decrementFilesOpenedForWrite();
    GridEventStorageManager evts = igfsCtx.kernalContext().event();
    if (evts.isRecordable(EVT_IGFS_FILE_CLOSED_WRITE))
        evts.record(new IgfsEvent(path, igfsCtx.localNode(), EVT_IGFS_FILE_CLOSED_WRITE, bytes));
}
Also used : GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) IgfsEvent(org.apache.ignite.events.IgfsEvent)

Aggregations

IgfsEvent (org.apache.ignite.events.IgfsEvent)17 ArrayList (java.util.ArrayList)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Event (org.apache.ignite.events.Event)9 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteException (org.apache.ignite.IgniteException)6 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)5 IgfsException (org.apache.ignite.igfs.IgfsException)4 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)3 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)3 IgfsConcurrentModificationException (org.apache.ignite.igfs.IgfsConcurrentModificationException)3 IgfsDirectoryNotEmptyException (org.apache.ignite.igfs.IgfsDirectoryNotEmptyException)3 IgfsParentNotDirectoryException (org.apache.ignite.igfs.IgfsParentNotDirectoryException)3 IgfsPath (org.apache.ignite.igfs.IgfsPath)3