Package ch.cern.dirq

Interface Queue

  • All Superinterfaces:
    java.lang.Iterable<java.lang.String>
    All Known Implementing Classes:
    QueueNull, QueueSimple

    public interface Queue
    extends java.lang.Iterable<java.lang.String>
    Queue - object oriented interface to a directory based queue.

    Description

    The goal of this module is to offer a queue system using the underlying filesystem for storage, security and to prevent race conditions via atomic operations. It focuses on simplicity, robustness and scalability.
    This module allows multiple concurrent readers and writers to interact with the same queue.
    Different implementations are available so readers and writers can be written in different programming languages:
    There is no knowledge of priority within a queue. If multiple priorities are needed, multiple queues should be used.

    Terminology

    An element is something that contains one or more pieces of data. With QueueSimple queues, an element can only contain one binary string.
    A queue is a "best effort" FIFO (First In - First Out) collection of elements.
    It is very hard to guarantee pure FIFO behavior with multiple writers using the same queue. Consider for instance:
    • Writer1: calls the add() method
    • Writer2: calls the add() method
    • Writer2: the add() method returns
    • Writer1: the add() method returns
    Who should be first in the queue, Writer1 or Writer2?
    For simplicity, this implementation provides only "best effort" FIFO, i.e. there is a very high probability that elements are processed in FIFO order but this is not guaranteed. This is achieved by using a high-resolution timer and having elements sorted by the time their final directory gets created.

    Locking

    Adding an element is not a problem because the add() method is atomic.
    In order to support multiple reader processes interacting with the same queue, advisory locking is used. Processes should first lock an element before working with it. In fact, the get() and remove() methods report a fatal error if they are called on unlocked elements.
    If the process that created the lock dies without unlocking the element, we end up with a staled lock. The purge() method can be used to remove these staled locks.
    An element can basically be in only one of two states: locked or unlocked.
    A newly created element is unlocked as a writer usually does not need to do anything more with it.
    Iterators return all the elements, regardless of their states.
    There is no method to get an element state as this information is usually useless since it may change at any time. Instead, programs should directly try to lock elements to make sure they are indeed locked.

    Security

    There are no specific security mechanisms in this module.
    The elements are stored as plain files and directories. The filesystem security features (owner, group, permissions, ACLs...) should be used to adequately protect the data.
    By default, the process' umask is respected. See the class constructor documentation if you want an other behavior.
    If multiple readers and writers with different uids are expected, the easiest solution is to have all the files and directories inside the toplevel directory world-writable (i.e. umask=0). Then, the permissions of the toplevel directory itself (e.g. group-writable) are enough to control who can access the queue.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.lang.String add​(byte[] data)
      Add byte array data to the queue.
      java.lang.String add​(java.lang.String data)
      Add String data to the queue.
      java.lang.String addPath​(java.lang.String path)
      Add the given file (identified by its path) to the queue and return the corresponding element name, the file must be on the same filesystem and will be moved to the queue.
      int count()
      Return the number of elements in the queue.
      java.lang.String get​(java.lang.String name)
      Get the given locked element as String data.
      byte[] getAsByteArray​(java.lang.String name)
      Get the given locked element as byte array data.
      java.lang.String getId()
      Return a unique identifier for the queue.
      java.lang.String getPath​(java.lang.String name)
      Get the path of the given locked element.
      java.lang.String getQueuePath()
      Return the path of the queue.
      boolean lock​(java.lang.String name)
      Lock an element in permissive mode.
      boolean lock​(java.lang.String name, boolean permissive)
      Lock an element.
      void purge()
      Purge the queue by removing unused intermediate directories, removing too old temporary elements and unlocking too old locked elements (aka staled locks); note: this can take a long time on queues with many elements.
      void purge​(int maxLock)
      Purge the queue by removing unused intermediate directories, removing too old temporary elements and unlocking too old locked elements (aka staled locks); note: this can take a long time on queues with many elements.
      void purge​(int maxLock, int maxTemp)
      Purge the queue by removing unused intermediate directories, removing too old temporary elements and unlocking too old locked elements (aka staled locks); note: this can take a long time on queues with many elements.
      void remove​(java.lang.String name)
      Remove a locked element from the queue.
      boolean unlock​(java.lang.String name)
      Unlock an element in non-permissive mode.
      boolean unlock​(java.lang.String name, boolean permissive)
      Unlock an element.
      • Methods inherited from interface java.lang.Iterable

        forEach, iterator, spliterator
    • Method Detail

      • getQueuePath

        java.lang.String getQueuePath()
        Return the path of the queue.
        Returns:
        queue path
      • getId

        java.lang.String getId()
        Return a unique identifier for the queue.
        Returns:
        unique queue identifier
      • add

        java.lang.String add​(java.lang.String data)
                      throws java.io.IOException
        Add String data to the queue.
        Parameters:
        data - data to be added
        Returns:
        element name (as directory_name/file_name)
        Throws:
        java.io.IOException - if any file operation fails
      • add

        java.lang.String add​(byte[] data)
                      throws java.io.IOException
        Add byte array data to the queue.
        Parameters:
        data - data to be added
        Returns:
        element name (as directory_name/file_name)
        Throws:
        java.io.IOException - if any file operation fails
      • addPath

        java.lang.String addPath​(java.lang.String path)
                          throws java.io.IOException
        Add the given file (identified by its path) to the queue and return the corresponding element name, the file must be on the same filesystem and will be moved to the queue.
        Parameters:
        path - path of the file to be added
        Returns:
        element name (as directory_name/file_name)
        Throws:
        java.io.IOException - if any file operation fails
      • get

        java.lang.String get​(java.lang.String name)
                      throws java.io.IOException
        Get the given locked element as String data.
        Parameters:
        name - name of the element to be retrieved
        Returns:
        data associated with the given element
        Throws:
        java.io.IOException
      • getAsByteArray

        byte[] getAsByteArray​(java.lang.String name)
                       throws java.io.IOException
        Get the given locked element as byte array data.
        Parameters:
        name - name of the element to be retrieved
        Returns:
        data associated with the given element
        Throws:
        java.io.IOException
      • getPath

        java.lang.String getPath​(java.lang.String name)
        Get the path of the given locked element.
        This pathFile can be read but not removed, you must use the remove() method for this purpose.
        Parameters:
        name - name of the element
        Returns:
        path of the element
      • lock

        boolean lock​(java.lang.String name)
              throws java.io.IOException
        Lock an element in permissive mode.
        Parameters:
        name - name of the element to be locked
        Returns:
        true on success, false if the element could not be locked
        Throws:
        java.io.IOException - if any file operation fails
      • lock

        boolean lock​(java.lang.String name,
                     boolean permissive)
              throws java.io.IOException
        Lock an element.
        Parameters:
        name - name of the element to be locked
        permissive - work in permissive mode
        Returns:
        true on success, false if the element could not be locked
        Throws:
        java.io.IOException - if any file operation fails
      • unlock

        boolean unlock​(java.lang.String name)
                throws java.io.IOException
        Unlock an element in non-permissive mode.
        Parameters:
        name - name of the element to be unlocked
        Returns:
        true on success, false if the element could not be unlocked
        Throws:
        java.io.IOException - if any file operation fails
      • unlock

        boolean unlock​(java.lang.String name,
                       boolean permissive)
                throws java.io.IOException
        Unlock an element.
        Parameters:
        name - name of the element to be unlocked
        permissive - work in permissive mode
        Returns:
        true on success, false if the element could not be unlocked
        Throws:
        java.io.IOException - if any file operation fails
      • remove

        void remove​(java.lang.String name)
             throws java.io.IOException
        Remove a locked element from the queue.
        Parameters:
        name - name of the element to be removed
        Throws:
        java.io.IOException - if any file operation fails
      • count

        int count()
        Return the number of elements in the queue.
        Locked elements are counted but temporary elements are not.
        Returns:
        number of elements in the queue
      • purge

        void purge()
            throws java.io.IOException
        Purge the queue by removing unused intermediate directories, removing too old temporary elements and unlocking too old locked elements (aka staled locks); note: this can take a long time on queues with many elements.
        It uses default value for maxTemp and maxLock
        Throws:
        java.io.IOException - if any file operation fails
      • purge

        void purge​(int maxLock)
            throws java.io.IOException
        Purge the queue by removing unused intermediate directories, removing too old temporary elements and unlocking too old locked elements (aka staled locks); note: this can take a long time on queues with many elements.
        Parameters:
        maxLock - maximum time for a locked element (in seconds); if set to 0, locked elements will not be unlocked; if set to null, the object's default value will be used
        Throws:
        java.io.IOException - if any file operation fails
      • purge

        void purge​(int maxLock,
                   int maxTemp)
            throws java.io.IOException
        Purge the queue by removing unused intermediate directories, removing too old temporary elements and unlocking too old locked elements (aka staled locks); note: this can take a long time on queues with many elements.
        Parameters:
        maxLock - maximum time for a locked element (in seconds); if set to 0, locked elements will not be unlocked; if set to null, the object's default value will be used
        maxTemp - maximum time for a temporary element (in seconds); if set to 0, temporary elements will not be removed if set to null, the object's default value will be used
        Throws:
        java.io.IOException - if any file operation fails