Performance Zone is brought to you in partnership with:

Dmitriy Setrakyan manages daily operations of GridGain Systems and brings over 12 years of experience to GridGain Systems which spans all areas of application software development from design and architecture to team management and quality assurance. His experience includes architecture and leadership in development of distributed middleware platforms, financial trading systems, CRM applications, and more. Dmitriy is a DZone MVB and is not an employee of DZone and has posted 57 posts at DZone. You can read more from them at their website. View Full User Profile

Efficient Techniques For Loading Data Into Memory

09.04.2013
| 5131 views |
  • submit to reddit

Data loading usually has to do with initializing cache data on start up. However, quite often caches need to be loaded or reloaded periodically, and not only on start up. In cases in which you need to load lots of data, either at start up or at any point afterward, using standard cache put(...) or putAll(...) operations is generally inefficient, especially when transactional boundaries are not important. This is especially true when data has to be partitioned across the network, so you don't know in advance on which node the data will end up.

For fast loading of large amounts of data, GridGain provides a cool mechanism called data loader (implemented via GridDataLoader). The data loader will properly batch keys together and collocate those batches with nodes on which the data will be cached. By controlling the size of the batch and the size of internal transactions it is possible to achieve very fast data loading rates.

The code below shows an example of how it can be done:

// Get the data loader reference.
try (GridDataLoader<Integer, String> ldr = grid.dataLoader("partitioned")) {
    // Load the entries.
    for (int i = 0; i < ENTRY_COUNT; i++)
        ldr.addData(i, Integer.toString(i));
}

Whenever the data is submitted to the data loader, it is stored in the buffer, which is consumed by loader threads. If the buffer is full, the user thread will block on the addData(...) call until the loader threads free enough room for new entries.

Another method of data preloading is to load it directly from a persistent data store. GridGain supports that via the GridCache.loadCache(...) method. Note that this method of loading data into cache is very efficient as it is local, non-transactional and is usually implemented using bulk data store operations. The reason it can afford to be non-transactional is because it will not override any values in cache, it can only insert new values. This means that if some transaction has already updated an entry, this entry will not be overwritten by the loadCache(...) call.

Whenever the GridCache.loadCache(...) method is called, it will internally delegate to the underlying persistent store implementation by invoking the GridCacheStore.loadAll(...) method. Usually implementation of this method will load from DB either full or partial set of objects depending on requirements.

Here is an example of how the GridCacheStore.loadAll(...) method may be implemented:

@Override
public void loadAll(@Nullable String cacheName, 
    GridInClosure2<UUID, Person> closure, Object... args) throws GridException {
    try (Connection conn = getConnection()) {
        // Load all Persons from database (perhaps to warm up cache?)
        try (PreparedStatement st = 
            conn.prepareStatement("select * from PERSONS")) {
            ResultSet rs = st.executeQuery();
  
            while (rs.next())
                c.apply(
                    // Key.
                    UUID.fromString(rs.getString(1)),
                    // New value.
                    person(rs.getString(1), rs.getString(2),
                        rs.getString(3), rs.getString(4))
                );
            }
        }
    }
    catch (SQLException e) {
        throw new GridException("Failed to load objects", e);
    }
}
Note that, instead of returning a collection of loaded entries, this method instead passes each load entry into the closure provided by the system, which avoids costly large collection creations and internal resizing. GridGain will then take the values passed into the closure and store them in cache.

Using the above loading routines will often render 10x and above performance improvement over simple put(...) calls.


Published at DZone with permission of Dmitriy Setrakyan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)