Batch Operations

  Search Here
Searching XAP 6.0 Documentation

                                               

Summary: An extension of the JavaSpaces API, enabling operations with multiple objects in one call.

Overview

Batch operations boost performance, since they interact with the server in one RPC call and retrieve the result from the space in one space operation. This allows the client and server to utilize the network bandwidth in efficient manner.

A few important points to note when using batch operations:

  • Some internal mechanisms within the space kernel use multiple threads when batch operations are called, so the overall performance boost when using batch operations can be relatively large compared to single operations – up to 10 times faster.
  • readMultiple should be handled with care since it can return a large data set (potentially all space data). This might cause an out of memory error in the space and client process. You should use ReturnOnlyUids mode or use the GSIterator to return the result in batches.
  • Destructive batch operations (take , write , update) should be performed with transactions – this allows the client to roll back the space to its initial state before the operation was started, in case of failure.
  • When simultaneously calling writeMultiple without a transaction and read with timeout or readMultiple, the read might return only some of the Entries written and indexed while it is being executed. This is because writeMultiple without a transaction is not atomic.
  • When calling a batch operation using UIDs, you should make sure null values (Entries or UIDs) are not part of the passed array.
  • You should verify that duplicated entries (with the same UID) do not appear as part of the passed array, since the identity of the Entries is determined based on their UIDs and not based on their reference. This is extremely important with an embedded space, since writeMultiple for Entries extending MetaDataEntry or ExternalEntry objects have their UID injected after the write operation.
  • readMultiple and takeMultiple do not support timeout operations. The simple way to achieve this is by calling the read operation with the proper timeout, and if non-null values are returned, perform the batch operation.
  • updateMultiple does not support timeout operation – you should check the returned array from the updateMultiple operation and ensure that you do not have null values. Null values means the Entry is blocked by another transaction. In this case you should retry the updateMultiple operation for the corresponding Entries.
    updateMultiple does not support the UPDATE_OR_WRITE modifier as part of GigaSpaces version 5.1. If you are using GigaSpaces version 5.2 and onwards, this limitation has been removed.

Operations Supported

com.j_spaces.core.IJSpace extends the net.jini.space.JavaSpace:

Return Value Method
Entry[]
readMultiple(Entry template,Transaction txn, int maxEntries)

Reads all the Entries matching the specified template from this space.

Entry[]
takeMultiple(Entry template,Transaction txn, int maxEntries)

Takes all the Entries matching the specified template from this space.

Object[]
updateMultiple(Entry entries,Transaction transaction, long leases)

Updates a group of Entries.

Object[]
updateMultiple(Entry entries,Transaction transaction, long leases, int updateModifiers)

Updates a group of Entries.

Lease[]
writeMultiple(Entry entries,Transaction txn, long lease)

Writes the specified Entries to this space.

Code Example

The following example demonstrates the usage of the different Javaspace batch operations:

package com.j_spaces.examples.j_spaces_adv.batch_operations;
import net.jini.core.entry.Entry;
import net.jini.core.lease.Lease;
import net.jini.space.JavaSpace;
import java.util.Arrays;
import com.j_spaces.core.IJSpace;
import com.j_spaces.core.client.FinderException;
import com.j_spaces.core.client.SpaceFinder;

public class JSBatchOperations {
    public static void main(String[] args) {
        Entry m_Template;
        IJSpace m_Space;
        Entry resultset[];

        int amount = 1000;

        System.out.println("\nWelcome to Gigaspaces Batch Operations Example!");
        System.out.println( "This example uses single- and batch-operations ...\n" );

        if (args.length != 1) {
            System.out.println("Usage: <spaceURL>");
            System.out.println("<protocol_name>://host:port/containername/spacename");
            System.exit(1);
        }
        try {
            m_Space = (IJSpace) SpaceFinder.find(args[0]);
            if (m_Space == null) {
                System.out.println("Space not found: " + args[0]);
                System.exit(-1);
            }

            System.out.println("Cleaning space...");
            m_Space.clear( null, null );

            // Use snapshot because this template is used several times
            m_Template = m_Space.snapshot(new Message());

            // Test WRITE operation (write vs. writeMultiple)
            say("\nWrite " + amount + " messages to space using single mode...");
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < amount ; i++) {
                Message msg = new Message();
                msg.content = "Hello World " + i;
                m_Space.write(msg, null, Lease.FOREVER);
            }
            showResult (startTime , "Write in single mode");

            say("Write " +amount +" messages to space using batch mode...");
            startTime = System.currentTimeMillis();
            Message msgs[] = new  Message [amount];
            for (int i = 0; i < amount ; i++) {
                Message msg = new Message();
                msg.content = "Hello World " + i;
                msgs[i] = msg;
            }
            m_Space.writeMultiple(msgs, null, Lease.FOREVER);
            showResult (startTime , "Write in batch mode");

            // Test UPDATE operation (update vs. updateMultiple)
            say("\nUpdate " +amount +" messages in space using single mode...");
            startTime = System.currentTimeMillis();
            for (int i = 0; i < amount; i++) {
                Message msg = new Message();
                msg.content = "Hello World " + i;
                Message ret = (Message) m_Space.read(msg, null, JavaSpace.NO_WAIT);
                m_Space.update(ret, null, Lease.FOREVER, 1000);
            }
            showResult (startTime , "Update in single mode");

            say("Update " +amount +" messages in space using batch mode...");
            startTime = System.currentTimeMillis();
            resultset = m_Space.readMultiple(m_Template, null, amount);
            long leases[] = new long[amount];
            Arrays.fill(leases , Lease.FOREVER);
            m_Space.updateMultiple(resultset , null , leases);
            showResult (startTime , "Update in batch mode");

            // Test TAKE operation (take vs. takeMultiple)
            say("\nTake " +amount +" messages from space using single mode...");
            startTime = System.currentTimeMillis();
            for (int i = 0; i < amount; i++) {
                Message msg = new Message();
                msg.content = "Hello World " + i;
                Message ret = (Message) m_Space.take(msg, null, JavaSpace.NO_WAIT);
            }
            showResult (startTime , "Take in single mode");

            say("Take " +amount +" messages from space using batch mode...");
            startTime = System.currentTimeMillis();
            resultset = m_Space.takeMultiple(m_Template, null, amount);
            showResult (startTime , "Take in batch mode");

            // Test READ operation (read vs. readMultiple)
            say("\nRead " +amount +" messages from space using single mode...");
            startTime = System.currentTimeMillis();
            for (int i = 0; i < amount; i++) {
                Message msg = new Message();
                msg.content = "Hello World " + i;
                Message ret = (Message) m_Space.read(msg, null, JavaSpace.NO_WAIT);
            }
            showResult (startTime , "Read in single mode");

            say("Read " +amount +" messages from space using batch mode...");
            startTime = System.currentTimeMillis();
            resultset = m_Space.readMultiple(m_Template, null, amount);
            showResult (startTime , "Read in batch mode");

            System.exit(0);
        }
        catch( FinderException ex ) {
            ex.printStackTrace();
            System.out.println("Could not find space: " + args[0]);
            System.out.println("Please check that GigaSpaces Server is running.");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void showResult(long startTime, String msg) {
        long endTime = System.currentTimeMillis();

        System.out.println(msg + ": " + (endTime - startTime) + " msec");
    }

    private static void say(String msg) {
        System.out.println(msg);
    }
}


GigaSpaces 6.0 Documentation Contents (Current Page in Bold)

    Java

    C++

    .NET

    Middleware Capabilities

    Configuration and Management

Add GigaSpaces wiki search to your browser search engines!
(works on Firefox 2 and Internet Explorer 7)

Labels