~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/terracotta/TerracottaBootstrapCacheLoader.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
16
16
 
17
17
package net.sf.ehcache.terracotta;
18
18
 
 
19
import java.io.IOException;
 
20
import java.util.Set;
 
21
 
19
22
import net.sf.ehcache.CacheException;
 
23
import net.sf.ehcache.CacheManager;
 
24
import net.sf.ehcache.DiskStorePathManager;
20
25
import net.sf.ehcache.Disposable;
21
26
import net.sf.ehcache.Ehcache;
22
27
import net.sf.ehcache.Status;
23
28
import net.sf.ehcache.distribution.RemoteCacheException;
24
29
import net.sf.ehcache.store.MemoryLimitedCacheLoader;
 
30
 
25
31
import org.slf4j.Logger;
26
32
import org.slf4j.LoggerFactory;
27
33
 
28
 
import java.io.IOException;
29
 
import java.util.Set;
30
 
 
31
34
/**
32
35
 * A {@link net.sf.ehcache.bootstrap.BootstrapCacheLoader} that will load Elements into a Terracotta clustered cache, based on a previously
33
36
 * snapshotted key set. It is also responsible to create snapshot files to disk
 
37
 *
34
38
 * @author Alex Snaps
35
39
 */
36
40
public class TerracottaBootstrapCacheLoader extends MemoryLimitedCacheLoader implements Disposable {
51
55
    private final boolean doKeySnapshot;
52
56
    private final boolean doKeySnapshotOnDedicatedThread;
53
57
    private final long interval;
54
 
    private final String directory;
 
58
    private final DiskStorePathManager diskStorePathManager;
55
59
 
56
60
    private volatile KeySnapshotter keySnapshotter;
57
61
    private volatile boolean immediateShutdown;
58
62
    private volatile boolean doKeySnapshotOnDispose;
59
63
 
60
 
    private TerracottaBootstrapCacheLoader(final boolean doKeySnapshot, final boolean aSynchronous,
61
 
                                           final String directory, final long interval, final boolean doKeySnapshotOnDedicatedThread) {
 
64
    private TerracottaBootstrapCacheLoader(final boolean doKeySnapshot, final boolean aSynchronous, final String directory,
 
65
            final long interval, final boolean doKeySnapshotOnDedicatedThread) {
62
66
        this.aSynchronous = aSynchronous;
63
67
        this.doKeySnapshot = doKeySnapshot;
64
68
        this.doKeySnapshotOnDedicatedThread = doKeySnapshotOnDedicatedThread;
65
69
        this.interval = interval;
66
 
        this.directory = directory;
 
70
        this.diskStorePathManager = directory != null ? new DiskStorePathManager(directory) : null;
67
71
    }
68
72
 
69
73
    /**
70
74
     * Constructor
 
75
     *
71
76
     * @param asynchronous do the loading asynchronously, or synchronously
72
77
     * @param directory the directory to read snapshot files from, and write them to
73
78
     * @param doKeySnapshots Whether to do keysnapshotting
78
83
 
79
84
    /**
80
85
     * Constructor
 
86
     *
81
87
     * @param asynchronous do the loading asynchronously, or synchronously
82
88
     * @param directory the directory to read snapshot files from, and write them to
83
89
     * @param interval the interval in seconds at which the snapshots of the local key set has to occur
88
94
 
89
95
    /**
90
96
     * Constructor
 
97
     *
91
98
     * @param asynchronous do the loading asynchronously, or synchronously
92
99
     * @param directory the directory to read snapshot files from, and write them to
93
100
     * @param interval the interval in seconds at which the snapshots of the local key set has to occur
94
101
     * @param onDedicatedThread whether to do the snapshot on a dedicated thread or using the CacheManager's
95
 
     * {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService}
 
102
     *            {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService}
96
103
     */
97
104
    public TerracottaBootstrapCacheLoader(final boolean asynchronous, String directory, long interval, boolean onDedicatedThread) {
98
105
        this(true, asynchronous, directory, interval, onDedicatedThread);
100
107
 
101
108
    /**
102
109
     * Whether the on going keysnapshot will finish before the instance is disposed
 
110
     *
103
111
     * @return true if disposable is immediate
104
112
     * @see Disposable
105
113
     */
110
118
    /**
111
119
     * Sets whether the disposal of the instance will let the potential current key set being written to disk finish, or whether the
112
120
     * shutdown will be immediate
 
121
     *
113
122
     * @param immediateShutdown true if immediate, false to let the snapshot finish
114
123
     */
115
124
    public void setImmediateShutdown(final boolean immediateShutdown) {
121
130
     */
122
131
    public void load(final Ehcache cache) throws CacheException {
123
132
        if (!cache.getCacheConfiguration().isTerracottaClustered()) {
124
 
            LOG.error("You're trying to bootstrap a non Terracotta clustered cache with a TerracottaBootstrapCacheLoader! Cache " +
125
 
                      "'{}' will not be bootstrapped and no keySet snapshot will be recorded...", cache.getName());
 
133
            LOG.error("You're trying to bootstrap a non Terracotta clustered cache with a TerracottaBootstrapCacheLoader! Cache "
 
134
                    + "'{}' will not be bootstrapped and no keySet snapshot will be recorded...", cache.getName());
126
135
            return;
127
136
        }
128
137
 
139
148
    }
140
149
 
141
150
    private void doLoad(final Ehcache cache) {
142
 
        final RotatingSnapshotFile snapshotFile = new RotatingSnapshotFile(directory == null ? cache.getCacheManager()
143
 
            .getDiskStorePath() : directory, cache.getName());
 
151
        CacheManager manager = cache.getCacheManager();
 
152
        if (manager == null) {
 
153
            throw new CacheException("Cache must belong to a cache manager to bootstrap");
 
154
        }
 
155
 
 
156
        DiskStorePathManager pathManager = diskStorePathManager != null ? diskStorePathManager : cache.getCacheManager()
 
157
                .getDiskStorePathManager();
 
158
 
 
159
        final RotatingSnapshotFile snapshotFile = new RotatingSnapshotFile(pathManager, cache.getName());
144
160
        try {
145
161
            final Set<Object> keys = snapshotFile.readAll();
146
162
            int loaded = 0;
152
168
                loaded++;
153
169
            }
154
170
            LOG.info("Finished loading {} keys (of {} on disk) from previous snapshot for Cache '{}'",
155
 
                new Object[] {Integer.valueOf(loaded), keys.size(), cache.getName()});
 
171
                    new Object[] {Integer.valueOf(loaded), keys.size(), cache.getName()});
156
172
        } catch (IOException e) {
157
173
            LOG.error("Couldn't load keySet for Cache '{}'", cache.getName(), e);
158
174
        }
186
202
                keySnapshotter.dispose(immediateShutdown);
187
203
            }
188
204
        }
 
205
        if (diskStorePathManager != null) {
 
206
            diskStorePathManager.releaseLock();
 
207
        }
189
208
    }
190
209
 
191
210
    /**
192
211
     * Calling this method will result in a snapshot being taken or wait for the one in progress to finish
 
212
     *
193
213
     * @throws IOException On exception being thrown while doing the snapshot
194
214
     */
195
215
    public void doLocalKeySnapshot() throws IOException {
206
226
 
207
227
    /**
208
228
     * Accessor to the associated {@link KeySnapshotter}
 
229
     *
209
230
     * @return the {@link KeySnapshotter} used by this loader instance
210
231
     */
211
232
    KeySnapshotter getKeySnapshotter() {
214
235
 
215
236
    /**
216
237
     * Configures the Loader to take a snapshot when it is being disposed
 
238
     *
217
239
     * @param doKeySnapshotOnDispose whether to snapshot on loader disposal
218
240
     */
219
241
    public void setSnapshotOnDispose(final boolean doKeySnapshotOnDispose) {
236
258
        /**
237
259
         * RemoteDebugger thread method.
238
260
         */
 
261
        @Override
239
262
        public final void run() {
240
263
            try {
241
264
                doLoad(cache);