001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.store.kahadb;
018
019import java.io.DataInputStream;
020import java.io.IOException;
021import java.io.InterruptedIOException;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Iterator;
026import java.util.LinkedList;
027import java.util.List;
028import java.util.Map;
029import java.util.Map.Entry;
030import java.util.Set;
031import java.util.concurrent.BlockingQueue;
032import java.util.concurrent.ExecutorService;
033import java.util.concurrent.FutureTask;
034import java.util.concurrent.LinkedBlockingQueue;
035import java.util.concurrent.Semaphore;
036import java.util.concurrent.ThreadFactory;
037import java.util.concurrent.ThreadPoolExecutor;
038import java.util.concurrent.TimeUnit;
039import java.util.concurrent.TimeoutException;
040import java.util.concurrent.atomic.AtomicBoolean;
041import java.util.concurrent.atomic.AtomicInteger;
042
043import org.apache.activemq.broker.ConnectionContext;
044import org.apache.activemq.broker.region.BaseDestination;
045import org.apache.activemq.broker.scheduler.JobSchedulerStore;
046import org.apache.activemq.command.ActiveMQDestination;
047import org.apache.activemq.command.ActiveMQQueue;
048import org.apache.activemq.command.ActiveMQTempQueue;
049import org.apache.activemq.command.ActiveMQTempTopic;
050import org.apache.activemq.command.ActiveMQTopic;
051import org.apache.activemq.command.Message;
052import org.apache.activemq.command.MessageAck;
053import org.apache.activemq.command.MessageId;
054import org.apache.activemq.command.ProducerId;
055import org.apache.activemq.command.SubscriptionInfo;
056import org.apache.activemq.command.TransactionId;
057import org.apache.activemq.openwire.OpenWireFormat;
058import org.apache.activemq.protobuf.Buffer;
059import org.apache.activemq.store.AbstractMessageStore;
060import org.apache.activemq.store.IndexListener;
061import org.apache.activemq.store.ListenableFuture;
062import org.apache.activemq.store.MessageRecoveryListener;
063import org.apache.activemq.store.MessageStore;
064import org.apache.activemq.store.MessageStoreStatistics;
065import org.apache.activemq.store.PersistenceAdapter;
066import org.apache.activemq.store.TopicMessageStore;
067import org.apache.activemq.store.TransactionIdTransformer;
068import org.apache.activemq.store.TransactionStore;
069import org.apache.activemq.store.kahadb.data.KahaAddMessageCommand;
070import org.apache.activemq.store.kahadb.data.KahaDestination;
071import org.apache.activemq.store.kahadb.data.KahaDestination.DestinationType;
072import org.apache.activemq.store.kahadb.data.KahaLocation;
073import org.apache.activemq.store.kahadb.data.KahaRemoveDestinationCommand;
074import org.apache.activemq.store.kahadb.data.KahaRemoveMessageCommand;
075import org.apache.activemq.store.kahadb.data.KahaSubscriptionCommand;
076import org.apache.activemq.store.kahadb.data.KahaUpdateMessageCommand;
077import org.apache.activemq.store.kahadb.disk.journal.Location;
078import org.apache.activemq.store.kahadb.disk.page.Transaction;
079import org.apache.activemq.store.kahadb.scheduler.JobSchedulerStoreImpl;
080import org.apache.activemq.usage.MemoryUsage;
081import org.apache.activemq.usage.SystemUsage;
082import org.apache.activemq.util.ServiceStopper;
083import org.apache.activemq.util.ThreadPoolUtils;
084import org.apache.activemq.wireformat.WireFormat;
085import org.slf4j.Logger;
086import org.slf4j.LoggerFactory;
087
088public class KahaDBStore extends MessageDatabase implements PersistenceAdapter {
089    static final Logger LOG = LoggerFactory.getLogger(KahaDBStore.class);
090    private static final int MAX_ASYNC_JOBS = BaseDestination.MAX_AUDIT_DEPTH;
091
092    public static final String PROPERTY_CANCELED_TASK_MOD_METRIC = "org.apache.activemq.store.kahadb.CANCELED_TASK_MOD_METRIC";
093    public static final int cancelledTaskModMetric = Integer.parseInt(System.getProperty(
094            PROPERTY_CANCELED_TASK_MOD_METRIC, "0"), 10);
095    public static final String PROPERTY_ASYNC_EXECUTOR_MAX_THREADS = "org.apache.activemq.store.kahadb.ASYNC_EXECUTOR_MAX_THREADS";
096    private static final int asyncExecutorMaxThreads = Integer.parseInt(System.getProperty(
097            PROPERTY_ASYNC_EXECUTOR_MAX_THREADS, "1"), 10);;
098
099    protected ExecutorService queueExecutor;
100    protected ExecutorService topicExecutor;
101    protected final List<Map<AsyncJobKey, StoreTask>> asyncQueueMaps = new LinkedList<Map<AsyncJobKey, StoreTask>>();
102    protected final List<Map<AsyncJobKey, StoreTask>> asyncTopicMaps = new LinkedList<Map<AsyncJobKey, StoreTask>>();
103    final WireFormat wireFormat = new OpenWireFormat();
104    private SystemUsage usageManager;
105    private LinkedBlockingQueue<Runnable> asyncQueueJobQueue;
106    private LinkedBlockingQueue<Runnable> asyncTopicJobQueue;
107    Semaphore globalQueueSemaphore;
108    Semaphore globalTopicSemaphore;
109    private boolean concurrentStoreAndDispatchQueues = true;
110    // when true, message order may be compromised when cache is exhausted if store is out
111    // or order w.r.t cache
112    private boolean concurrentStoreAndDispatchTopics = false;
113    private final boolean concurrentStoreAndDispatchTransactions = false;
114    private int maxAsyncJobs = MAX_ASYNC_JOBS;
115    private final KahaDBTransactionStore transactionStore;
116    private TransactionIdTransformer transactionIdTransformer;
117
118    public KahaDBStore() {
119        this.transactionStore = new KahaDBTransactionStore(this);
120        this.transactionIdTransformer = new TransactionIdTransformer() {
121            @Override
122            public TransactionId transform(TransactionId txid) {
123                return txid;
124            }
125        };
126    }
127
128    @Override
129    public String toString() {
130        return "KahaDB:[" + directory.getAbsolutePath() + "]";
131    }
132
133    @Override
134    public void setBrokerName(String brokerName) {
135    }
136
137    @Override
138    public void setUsageManager(SystemUsage usageManager) {
139        this.usageManager = usageManager;
140    }
141
142    public SystemUsage getUsageManager() {
143        return this.usageManager;
144    }
145
146    /**
147     * @return the concurrentStoreAndDispatch
148     */
149    public boolean isConcurrentStoreAndDispatchQueues() {
150        return this.concurrentStoreAndDispatchQueues;
151    }
152
153    /**
154     * @param concurrentStoreAndDispatch
155     *            the concurrentStoreAndDispatch to set
156     */
157    public void setConcurrentStoreAndDispatchQueues(boolean concurrentStoreAndDispatch) {
158        this.concurrentStoreAndDispatchQueues = concurrentStoreAndDispatch;
159    }
160
161    /**
162     * @return the concurrentStoreAndDispatch
163     */
164    public boolean isConcurrentStoreAndDispatchTopics() {
165        return this.concurrentStoreAndDispatchTopics;
166    }
167
168    /**
169     * @param concurrentStoreAndDispatch
170     *            the concurrentStoreAndDispatch to set
171     */
172    public void setConcurrentStoreAndDispatchTopics(boolean concurrentStoreAndDispatch) {
173        this.concurrentStoreAndDispatchTopics = concurrentStoreAndDispatch;
174    }
175
176    public boolean isConcurrentStoreAndDispatchTransactions() {
177        return this.concurrentStoreAndDispatchTransactions;
178    }
179
180    /**
181     * @return the maxAsyncJobs
182     */
183    public int getMaxAsyncJobs() {
184        return this.maxAsyncJobs;
185    }
186
187    /**
188     * @param maxAsyncJobs
189     *            the maxAsyncJobs to set
190     */
191    public void setMaxAsyncJobs(int maxAsyncJobs) {
192        this.maxAsyncJobs = maxAsyncJobs;
193    }
194
195    @Override
196    public void doStart() throws Exception {
197        if (brokerService != null) {
198            metadata.openwireVersion = brokerService.getStoreOpenWireVersion();
199            wireFormat.setVersion(metadata.openwireVersion);
200
201            if (LOG.isDebugEnabled()) {
202                LOG.debug("Store OpenWire version configured as: {}", metadata.openwireVersion);
203            }
204
205        }
206        super.doStart();
207
208        if (brokerService != null) {
209            // In case the recovered store used a different OpenWire version log a warning
210            // to assist in determining why journal reads fail.
211            if (metadata.openwireVersion != brokerService.getStoreOpenWireVersion()) {
212                LOG.warn("Existing Store uses a different OpenWire version[{}] " +
213                         "than the version configured[{}] reverting to the version " +
214                         "used by this store, some newer broker features may not work" +
215                         "as expected.",
216                         metadata.openwireVersion, brokerService.getStoreOpenWireVersion());
217
218                // Update the broker service instance to the actual version in use.
219                wireFormat.setVersion(metadata.openwireVersion);
220                brokerService.setStoreOpenWireVersion(metadata.openwireVersion);
221            }
222        }
223
224        this.globalQueueSemaphore = new Semaphore(getMaxAsyncJobs());
225        this.globalTopicSemaphore = new Semaphore(getMaxAsyncJobs());
226        this.asyncQueueJobQueue = new LinkedBlockingQueue<Runnable>(getMaxAsyncJobs());
227        this.asyncTopicJobQueue = new LinkedBlockingQueue<Runnable>(getMaxAsyncJobs());
228        this.queueExecutor = new StoreTaskExecutor(1, asyncExecutorMaxThreads, 0L, TimeUnit.MILLISECONDS,
229            asyncQueueJobQueue, new ThreadFactory() {
230                @Override
231                public Thread newThread(Runnable runnable) {
232                    Thread thread = new Thread(runnable, "ConcurrentQueueStoreAndDispatch");
233                    thread.setDaemon(true);
234                    return thread;
235                }
236            });
237        this.topicExecutor = new StoreTaskExecutor(1, asyncExecutorMaxThreads, 0L, TimeUnit.MILLISECONDS,
238            asyncTopicJobQueue, new ThreadFactory() {
239                @Override
240                public Thread newThread(Runnable runnable) {
241                    Thread thread = new Thread(runnable, "ConcurrentTopicStoreAndDispatch");
242                    thread.setDaemon(true);
243                    return thread;
244                }
245            });
246    }
247
248    @Override
249    public void doStop(ServiceStopper stopper) throws Exception {
250        // drain down async jobs
251        LOG.info("Stopping async queue tasks");
252        if (this.globalQueueSemaphore != null) {
253            this.globalQueueSemaphore.tryAcquire(this.maxAsyncJobs, 60, TimeUnit.SECONDS);
254        }
255        synchronized (this.asyncQueueMaps) {
256            for (Map<AsyncJobKey, StoreTask> m : asyncQueueMaps) {
257                synchronized (m) {
258                    for (StoreTask task : m.values()) {
259                        task.cancel();
260                    }
261                }
262            }
263            this.asyncQueueMaps.clear();
264        }
265        LOG.info("Stopping async topic tasks");
266        if (this.globalTopicSemaphore != null) {
267            this.globalTopicSemaphore.tryAcquire(this.maxAsyncJobs, 60, TimeUnit.SECONDS);
268        }
269        synchronized (this.asyncTopicMaps) {
270            for (Map<AsyncJobKey, StoreTask> m : asyncTopicMaps) {
271                synchronized (m) {
272                    for (StoreTask task : m.values()) {
273                        task.cancel();
274                    }
275                }
276            }
277            this.asyncTopicMaps.clear();
278        }
279        if (this.globalQueueSemaphore != null) {
280            this.globalQueueSemaphore.drainPermits();
281        }
282        if (this.globalTopicSemaphore != null) {
283            this.globalTopicSemaphore.drainPermits();
284        }
285        if (this.queueExecutor != null) {
286            ThreadPoolUtils.shutdownNow(queueExecutor);
287            queueExecutor = null;
288        }
289        if (this.topicExecutor != null) {
290            ThreadPoolUtils.shutdownNow(topicExecutor);
291            topicExecutor = null;
292        }
293        LOG.info("Stopped KahaDB");
294        super.doStop(stopper);
295    }
296
297    private Location findMessageLocation(final String key, final KahaDestination destination) throws IOException {
298        return pageFile.tx().execute(new Transaction.CallableClosure<Location, IOException>() {
299            @Override
300            public Location execute(Transaction tx) throws IOException {
301                StoredDestination sd = getStoredDestination(destination, tx);
302                Long sequence = sd.messageIdIndex.get(tx, key);
303                if (sequence == null) {
304                    return null;
305                }
306                return sd.orderIndex.get(tx, sequence).location;
307            }
308        });
309    }
310
311    protected StoreQueueTask removeQueueTask(KahaDBMessageStore store, MessageId id) {
312        StoreQueueTask task = null;
313        synchronized (store.asyncTaskMap) {
314            task = (StoreQueueTask) store.asyncTaskMap.remove(new AsyncJobKey(id, store.getDestination()));
315        }
316        return task;
317    }
318
319    protected void addQueueTask(KahaDBMessageStore store, StoreQueueTask task) throws IOException {
320        synchronized (store.asyncTaskMap) {
321            store.asyncTaskMap.put(new AsyncJobKey(task.getMessage().getMessageId(), store.getDestination()), task);
322        }
323        this.queueExecutor.execute(task);
324    }
325
326    protected StoreTopicTask removeTopicTask(KahaDBTopicMessageStore store, MessageId id) {
327        StoreTopicTask task = null;
328        synchronized (store.asyncTaskMap) {
329            task = (StoreTopicTask) store.asyncTaskMap.remove(new AsyncJobKey(id, store.getDestination()));
330        }
331        return task;
332    }
333
334    protected void addTopicTask(KahaDBTopicMessageStore store, StoreTopicTask task) throws IOException {
335        synchronized (store.asyncTaskMap) {
336            store.asyncTaskMap.put(new AsyncJobKey(task.getMessage().getMessageId(), store.getDestination()), task);
337        }
338        this.topicExecutor.execute(task);
339    }
340
341    @Override
342    public TransactionStore createTransactionStore() throws IOException {
343        return this.transactionStore;
344    }
345
346    public boolean getForceRecoverIndex() {
347        return this.forceRecoverIndex;
348    }
349
350    public void setForceRecoverIndex(boolean forceRecoverIndex) {
351        this.forceRecoverIndex = forceRecoverIndex;
352    }
353
354    public class KahaDBMessageStore extends AbstractMessageStore {
355        protected final Map<AsyncJobKey, StoreTask> asyncTaskMap = new HashMap<AsyncJobKey, StoreTask>();
356        protected KahaDestination dest;
357        private final int maxAsyncJobs;
358        private final Semaphore localDestinationSemaphore;
359
360        double doneTasks, canceledTasks = 0;
361
362        public KahaDBMessageStore(ActiveMQDestination destination) {
363            super(destination);
364            this.dest = convert(destination);
365            this.maxAsyncJobs = getMaxAsyncJobs();
366            this.localDestinationSemaphore = new Semaphore(this.maxAsyncJobs);
367        }
368
369        @Override
370        public ActiveMQDestination getDestination() {
371            return destination;
372        }
373
374        @Override
375        public ListenableFuture<Object> asyncAddQueueMessage(final ConnectionContext context, final Message message)
376                throws IOException {
377            if (isConcurrentStoreAndDispatchQueues()) {
378                StoreQueueTask result = new StoreQueueTask(this, context, message);
379                ListenableFuture<Object> future = result.getFuture();
380                message.getMessageId().setFutureOrSequenceLong(future);
381                message.setRecievedByDFBridge(true); // flag message as concurrentStoreAndDispatch
382                result.aquireLocks();
383                addQueueTask(this, result);
384                if (indexListener != null) {
385                    indexListener.onAdd(new IndexListener.MessageContext(context, message, null));
386                }
387                return future;
388            } else {
389                return super.asyncAddQueueMessage(context, message);
390            }
391        }
392
393        @Override
394        public void removeAsyncMessage(ConnectionContext context, MessageAck ack) throws IOException {
395            if (isConcurrentStoreAndDispatchQueues()) {
396                AsyncJobKey key = new AsyncJobKey(ack.getLastMessageId(), getDestination());
397                StoreQueueTask task = null;
398                synchronized (asyncTaskMap) {
399                    task = (StoreQueueTask) asyncTaskMap.get(key);
400                }
401                if (task != null) {
402                    if (ack.isInTransaction() || !task.cancel()) {
403                        try {
404                            task.future.get();
405                        } catch (InterruptedException e) {
406                            throw new InterruptedIOException(e.toString());
407                        } catch (Exception ignored) {
408                            LOG.debug("removeAsync: cannot cancel, waiting for add resulted in ex", ignored);
409                        }
410                        removeMessage(context, ack);
411                    } else {
412                        synchronized (asyncTaskMap) {
413                            asyncTaskMap.remove(key);
414                        }
415                    }
416                } else {
417                    removeMessage(context, ack);
418                }
419            } else {
420                removeMessage(context, ack);
421            }
422        }
423
424        @Override
425        public void addMessage(final ConnectionContext context, final Message message) throws IOException {
426            final KahaAddMessageCommand command = new KahaAddMessageCommand();
427            command.setDestination(dest);
428            command.setMessageId(message.getMessageId().toProducerKey());
429            command.setTransactionInfo(TransactionIdConversion.convert(transactionIdTransformer.transform(message.getTransactionId())));
430            command.setPriority(message.getPriority());
431            command.setPrioritySupported(isPrioritizedMessages());
432            org.apache.activemq.util.ByteSequence packet = wireFormat.marshal(message);
433            command.setMessage(new Buffer(packet.getData(), packet.getOffset(), packet.getLength()));
434            store(command, isEnableJournalDiskSyncs() && message.isResponseRequired(), new IndexAware() {
435                // sync add? (for async, future present from getFutureOrSequenceLong)
436                Object possibleFuture = message.getMessageId().getFutureOrSequenceLong();
437
438                @Override
439                public void sequenceAssignedWithIndexLocked(final long sequence) {
440                    message.getMessageId().setFutureOrSequenceLong(sequence);
441                    if (indexListener != null) {
442                        if (possibleFuture == null) {
443                            trackPendingAdd(dest, sequence);
444                            indexListener.onAdd(new IndexListener.MessageContext(context, message, new Runnable() {
445                                @Override
446                                public void run() {
447                                    trackPendingAddComplete(dest, sequence);
448                                }
449                            }));
450                        }
451                    }
452                }
453            }, null);
454        }
455
456        @Override
457        public void updateMessage(Message message) throws IOException {
458            if (LOG.isTraceEnabled()) {
459                LOG.trace("updating: " + message.getMessageId() + " with deliveryCount: " + message.getRedeliveryCounter());
460            }
461            KahaUpdateMessageCommand updateMessageCommand = new KahaUpdateMessageCommand();
462            KahaAddMessageCommand command = new KahaAddMessageCommand();
463            command.setDestination(dest);
464            command.setMessageId(message.getMessageId().toProducerKey());
465            command.setPriority(message.getPriority());
466            command.setPrioritySupported(prioritizedMessages);
467            org.apache.activemq.util.ByteSequence packet = wireFormat.marshal(message);
468            command.setMessage(new Buffer(packet.getData(), packet.getOffset(), packet.getLength()));
469            updateMessageCommand.setMessage(command);
470            store(updateMessageCommand, isEnableJournalDiskSyncs(), null, null);
471        }
472
473        @Override
474        public void removeMessage(ConnectionContext context, MessageAck ack) throws IOException {
475            KahaRemoveMessageCommand command = new KahaRemoveMessageCommand();
476            command.setDestination(dest);
477            command.setMessageId(ack.getLastMessageId().toProducerKey());
478            command.setTransactionInfo(TransactionIdConversion.convert(transactionIdTransformer.transform(ack.getTransactionId())));
479
480            org.apache.activemq.util.ByteSequence packet = wireFormat.marshal(ack);
481            command.setAck(new Buffer(packet.getData(), packet.getOffset(), packet.getLength()));
482            store(command, isEnableJournalDiskSyncs() && ack.isResponseRequired(), null, null);
483        }
484
485        @Override
486        public void removeAllMessages(ConnectionContext context) throws IOException {
487            KahaRemoveDestinationCommand command = new KahaRemoveDestinationCommand();
488            command.setDestination(dest);
489            store(command, true, null, null);
490        }
491
492        @Override
493        public Message getMessage(MessageId identity) throws IOException {
494            final String key = identity.toProducerKey();
495
496            // Hopefully one day the page file supports concurrent read
497            // operations... but for now we must
498            // externally synchronize...
499            Location location;
500            indexLock.writeLock().lock();
501            try {
502                location = findMessageLocation(key, dest);
503            } finally {
504                indexLock.writeLock().unlock();
505            }
506            if (location == null) {
507                return null;
508            }
509
510            return loadMessage(location);
511        }
512
513        @Override
514        public boolean isEmpty() throws IOException {
515            indexLock.writeLock().lock();
516            try {
517                return pageFile.tx().execute(new Transaction.CallableClosure<Boolean, IOException>() {
518                    @Override
519                    public Boolean execute(Transaction tx) throws IOException {
520                        // Iterate through all index entries to get a count of
521                        // messages in the destination.
522                        StoredDestination sd = getStoredDestination(dest, tx);
523                        return sd.locationIndex.isEmpty(tx);
524                    }
525                });
526            } finally {
527                indexLock.writeLock().unlock();
528            }
529        }
530
531        @Override
532        public void recover(final MessageRecoveryListener listener) throws Exception {
533            // recovery may involve expiry which will modify
534            indexLock.writeLock().lock();
535            try {
536                pageFile.tx().execute(new Transaction.Closure<Exception>() {
537                    @Override
538                    public void execute(Transaction tx) throws Exception {
539                        StoredDestination sd = getStoredDestination(dest, tx);
540                        recoverRolledBackAcks(sd, tx, Integer.MAX_VALUE, listener);
541                        sd.orderIndex.resetCursorPosition();
542                        for (Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx); listener.hasSpace() && iterator
543                                .hasNext(); ) {
544                            Entry<Long, MessageKeys> entry = iterator.next();
545                            if (ackedAndPrepared.contains(entry.getValue().messageId)) {
546                                continue;
547                            }
548                            Message msg = loadMessage(entry.getValue().location);
549                            listener.recoverMessage(msg);
550                        }
551                    }
552                });
553            } finally {
554                indexLock.writeLock().unlock();
555            }
556        }
557
558        @Override
559        public void recoverNextMessages(final int maxReturned, final MessageRecoveryListener listener) throws Exception {
560            indexLock.writeLock().lock();
561            try {
562                pageFile.tx().execute(new Transaction.Closure<Exception>() {
563                    @Override
564                    public void execute(Transaction tx) throws Exception {
565                        StoredDestination sd = getStoredDestination(dest, tx);
566                        Entry<Long, MessageKeys> entry = null;
567                        int counter = recoverRolledBackAcks(sd, tx, maxReturned, listener);
568                        for (Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx); iterator.hasNext(); ) {
569                            entry = iterator.next();
570                            if (ackedAndPrepared.contains(entry.getValue().messageId)) {
571                                continue;
572                            }
573                            Message msg = loadMessage(entry.getValue().location);
574                            msg.getMessageId().setFutureOrSequenceLong(entry.getKey());
575                            listener.recoverMessage(msg);
576                            counter++;
577                            if (counter >= maxReturned) {
578                                break;
579                            }
580                        }
581                        sd.orderIndex.stoppedIterating();
582                    }
583                });
584            } finally {
585                indexLock.writeLock().unlock();
586            }
587        }
588
589        protected int recoverRolledBackAcks(StoredDestination sd, Transaction tx, int maxReturned, MessageRecoveryListener listener) throws Exception {
590            int counter = 0;
591            String id;
592            for (Iterator<String> iterator = rolledBackAcks.iterator(); iterator.hasNext(); ) {
593                id = iterator.next();
594                iterator.remove();
595                Long sequence = sd.messageIdIndex.get(tx, id);
596                if (sequence != null) {
597                    if (sd.orderIndex.alreadyDispatched(sequence)) {
598                        listener.recoverMessage(loadMessage(sd.orderIndex.get(tx, sequence).location));
599                        counter++;
600                        if (counter >= maxReturned) {
601                            break;
602                        }
603                    } else {
604                        LOG.info("rolledback ack message {} with seq {} will be picked up in future batch {}", id, sequence, sd.orderIndex.cursor);
605                    }
606                } else {
607                    LOG.warn("Failed to locate rolled back ack message {} in {}", id, sd);
608                }
609            }
610            return counter;
611        }
612
613
614        @Override
615        public void resetBatching() {
616            if (pageFile.isLoaded()) {
617                indexLock.writeLock().lock();
618                try {
619                    pageFile.tx().execute(new Transaction.Closure<Exception>() {
620                        @Override
621                        public void execute(Transaction tx) throws Exception {
622                            StoredDestination sd = getExistingStoredDestination(dest, tx);
623                            if (sd != null) {
624                                sd.orderIndex.resetCursorPosition();}
625                            }
626                        });
627                } catch (Exception e) {
628                    LOG.error("Failed to reset batching",e);
629                } finally {
630                    indexLock.writeLock().unlock();
631                }
632            }
633        }
634
635        @Override
636        public void setBatch(final MessageId identity) throws IOException {
637            indexLock.writeLock().lock();
638            try {
639                pageFile.tx().execute(new Transaction.Closure<IOException>() {
640                    @Override
641                    public void execute(Transaction tx) throws IOException {
642                        StoredDestination sd = getStoredDestination(dest, tx);
643                        Long location = (Long) identity.getFutureOrSequenceLong();
644                        Long pending = sd.orderIndex.minPendingAdd();
645                        if (pending != null) {
646                            location = Math.min(location, pending-1);
647                        }
648                        sd.orderIndex.setBatch(tx, location);
649                    }
650                });
651            } finally {
652                indexLock.writeLock().unlock();
653            }
654        }
655
656        @Override
657        public void setMemoryUsage(MemoryUsage memoryUsage) {
658        }
659        @Override
660        public void start() throws Exception {
661            super.start();
662        }
663        @Override
664        public void stop() throws Exception {
665            super.stop();
666        }
667
668        protected void lockAsyncJobQueue() {
669            try {
670                if (!this.localDestinationSemaphore.tryAcquire(this.maxAsyncJobs, 60, TimeUnit.SECONDS)) {
671                    throw new TimeoutException(this +" timeout waiting for localDestSem:" + this.localDestinationSemaphore);
672                }
673            } catch (Exception e) {
674                LOG.error("Failed to lock async jobs for " + this.destination, e);
675            }
676        }
677
678        protected void unlockAsyncJobQueue() {
679            this.localDestinationSemaphore.release(this.maxAsyncJobs);
680        }
681
682        protected void acquireLocalAsyncLock() {
683            try {
684                this.localDestinationSemaphore.acquire();
685            } catch (InterruptedException e) {
686                LOG.error("Failed to aquire async lock for " + this.destination, e);
687            }
688        }
689
690        protected void releaseLocalAsyncLock() {
691            this.localDestinationSemaphore.release();
692        }
693
694        @Override
695        public String toString(){
696            return "permits:" + this.localDestinationSemaphore.availablePermits() + ",sd=" + storedDestinations.get(key(dest));
697        }
698
699        @Override
700        protected void recoverMessageStoreStatistics() throws IOException {
701            try {
702                MessageStoreStatistics recoveredStatistics;
703                lockAsyncJobQueue();
704                indexLock.writeLock().lock();
705                try {
706                    recoveredStatistics = pageFile.tx().execute(new Transaction.CallableClosure<MessageStoreStatistics, IOException>() {
707                        @Override
708                        public MessageStoreStatistics execute(Transaction tx) throws IOException {
709                            MessageStoreStatistics statistics = new MessageStoreStatistics();
710
711                            // Iterate through all index entries to get the size of each message
712                            StoredDestination sd = getStoredDestination(dest, tx);
713                            for (Iterator<Entry<Location, Long>> iterator = sd.locationIndex.iterator(tx); iterator.hasNext();) {
714                                int locationSize = iterator.next().getKey().getSize();
715                                statistics.getMessageCount().increment();
716                                statistics.getMessageSize().addSize(locationSize > 0 ? locationSize : 0);
717                            }
718                           return statistics;
719                        }
720                    });
721                    getMessageStoreStatistics().getMessageCount().setCount(recoveredStatistics.getMessageCount().getCount());
722                    getMessageStoreStatistics().getMessageSize().setTotalSize(recoveredStatistics.getMessageSize().getTotalSize());
723                } finally {
724                    indexLock.writeLock().unlock();
725                }
726            } finally {
727                unlockAsyncJobQueue();
728            }
729        }
730    }
731
732    class KahaDBTopicMessageStore extends KahaDBMessageStore implements TopicMessageStore {
733        private final AtomicInteger subscriptionCount = new AtomicInteger();
734        public KahaDBTopicMessageStore(ActiveMQTopic destination) throws IOException {
735            super(destination);
736            this.subscriptionCount.set(getAllSubscriptions().length);
737            if (isConcurrentStoreAndDispatchTopics()) {
738                asyncTopicMaps.add(asyncTaskMap);
739            }
740        }
741
742        @Override
743        public ListenableFuture<Object> asyncAddTopicMessage(final ConnectionContext context, final Message message)
744                throws IOException {
745            if (isConcurrentStoreAndDispatchTopics()) {
746                StoreTopicTask result = new StoreTopicTask(this, context, message, subscriptionCount.get());
747                result.aquireLocks();
748                addTopicTask(this, result);
749                return result.getFuture();
750            } else {
751                return super.asyncAddTopicMessage(context, message);
752            }
753        }
754
755        @Override
756        public void acknowledge(ConnectionContext context, String clientId, String subscriptionName,
757                                MessageId messageId, MessageAck ack) throws IOException {
758            String subscriptionKey = subscriptionKey(clientId, subscriptionName).toString();
759            if (isConcurrentStoreAndDispatchTopics()) {
760                AsyncJobKey key = new AsyncJobKey(messageId, getDestination());
761                StoreTopicTask task = null;
762                synchronized (asyncTaskMap) {
763                    task = (StoreTopicTask) asyncTaskMap.get(key);
764                }
765                if (task != null) {
766                    if (task.addSubscriptionKey(subscriptionKey)) {
767                        removeTopicTask(this, messageId);
768                        if (task.cancel()) {
769                            synchronized (asyncTaskMap) {
770                                asyncTaskMap.remove(key);
771                            }
772                        }
773                    }
774                } else {
775                    doAcknowledge(context, subscriptionKey, messageId, ack);
776                }
777            } else {
778                doAcknowledge(context, subscriptionKey, messageId, ack);
779            }
780        }
781
782        protected void doAcknowledge(ConnectionContext context, String subscriptionKey, MessageId messageId, MessageAck ack)
783                throws IOException {
784            KahaRemoveMessageCommand command = new KahaRemoveMessageCommand();
785            command.setDestination(dest);
786            command.setSubscriptionKey(subscriptionKey);
787            command.setMessageId(messageId.toProducerKey());
788            command.setTransactionInfo(ack != null ? TransactionIdConversion.convert(transactionIdTransformer.transform(ack.getTransactionId())) : null);
789            if (ack != null && ack.isUnmatchedAck()) {
790                command.setAck(UNMATCHED);
791            } else {
792                org.apache.activemq.util.ByteSequence packet = wireFormat.marshal(ack);
793                command.setAck(new Buffer(packet.getData(), packet.getOffset(), packet.getLength()));
794            }
795            store(command, false, null, null);
796        }
797
798        @Override
799        public void addSubscription(SubscriptionInfo subscriptionInfo, boolean retroactive) throws IOException {
800            String subscriptionKey = subscriptionKey(subscriptionInfo.getClientId(), subscriptionInfo
801                    .getSubscriptionName());
802            KahaSubscriptionCommand command = new KahaSubscriptionCommand();
803            command.setDestination(dest);
804            command.setSubscriptionKey(subscriptionKey.toString());
805            command.setRetroactive(retroactive);
806            org.apache.activemq.util.ByteSequence packet = wireFormat.marshal(subscriptionInfo);
807            command.setSubscriptionInfo(new Buffer(packet.getData(), packet.getOffset(), packet.getLength()));
808            store(command, isEnableJournalDiskSyncs() && true, null, null);
809            this.subscriptionCount.incrementAndGet();
810        }
811
812        @Override
813        public void deleteSubscription(String clientId, String subscriptionName) throws IOException {
814            KahaSubscriptionCommand command = new KahaSubscriptionCommand();
815            command.setDestination(dest);
816            command.setSubscriptionKey(subscriptionKey(clientId, subscriptionName).toString());
817            store(command, isEnableJournalDiskSyncs() && true, null, null);
818            this.subscriptionCount.decrementAndGet();
819        }
820
821        @Override
822        public SubscriptionInfo[] getAllSubscriptions() throws IOException {
823
824            final ArrayList<SubscriptionInfo> subscriptions = new ArrayList<SubscriptionInfo>();
825            indexLock.writeLock().lock();
826            try {
827                pageFile.tx().execute(new Transaction.Closure<IOException>() {
828                    @Override
829                    public void execute(Transaction tx) throws IOException {
830                        StoredDestination sd = getStoredDestination(dest, tx);
831                        for (Iterator<Entry<String, KahaSubscriptionCommand>> iterator = sd.subscriptions.iterator(tx); iterator
832                                .hasNext();) {
833                            Entry<String, KahaSubscriptionCommand> entry = iterator.next();
834                            SubscriptionInfo info = (SubscriptionInfo) wireFormat.unmarshal(new DataInputStream(entry
835                                    .getValue().getSubscriptionInfo().newInput()));
836                            subscriptions.add(info);
837
838                        }
839                    }
840                });
841            } finally {
842                indexLock.writeLock().unlock();
843            }
844
845            SubscriptionInfo[] rc = new SubscriptionInfo[subscriptions.size()];
846            subscriptions.toArray(rc);
847            return rc;
848        }
849
850        @Override
851        public SubscriptionInfo lookupSubscription(String clientId, String subscriptionName) throws IOException {
852            final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
853            indexLock.writeLock().lock();
854            try {
855                return pageFile.tx().execute(new Transaction.CallableClosure<SubscriptionInfo, IOException>() {
856                    @Override
857                    public SubscriptionInfo execute(Transaction tx) throws IOException {
858                        StoredDestination sd = getStoredDestination(dest, tx);
859                        KahaSubscriptionCommand command = sd.subscriptions.get(tx, subscriptionKey);
860                        if (command == null) {
861                            return null;
862                        }
863                        return (SubscriptionInfo) wireFormat.unmarshal(new DataInputStream(command
864                                .getSubscriptionInfo().newInput()));
865                    }
866                });
867            } finally {
868                indexLock.writeLock().unlock();
869            }
870        }
871
872        @Override
873        public int getMessageCount(String clientId, String subscriptionName) throws IOException {
874            final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
875            indexLock.writeLock().lock();
876            try {
877                return pageFile.tx().execute(new Transaction.CallableClosure<Integer, IOException>() {
878                    @Override
879                    public Integer execute(Transaction tx) throws IOException {
880                        StoredDestination sd = getStoredDestination(dest, tx);
881                        LastAck cursorPos = getLastAck(tx, sd, subscriptionKey);
882                        if (cursorPos == null) {
883                            // The subscription might not exist.
884                            return 0;
885                        }
886
887                        return (int) getStoredMessageCount(tx, sd, subscriptionKey);
888                    }
889                });
890            } finally {
891                indexLock.writeLock().unlock();
892            }
893        }
894
895
896        @Override
897        public long getMessageSize(String clientId, String subscriptionName) throws IOException {
898            final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
899            indexLock.writeLock().lock();
900            try {
901                return pageFile.tx().execute(new Transaction.CallableClosure<Integer, IOException>() {
902                    @Override
903                    public Integer execute(Transaction tx) throws IOException {
904                        StoredDestination sd = getStoredDestination(dest, tx);
905                        LastAck cursorPos = getLastAck(tx, sd, subscriptionKey);
906                        if (cursorPos == null) {
907                            // The subscription might not exist.
908                            return 0;
909                        }
910
911                        return (int) getStoredMessageSize(tx, sd, subscriptionKey);
912                    }
913                });
914            } finally {
915                indexLock.writeLock().unlock();
916            }
917        }
918
919        @Override
920        public void recoverSubscription(String clientId, String subscriptionName, final MessageRecoveryListener listener)
921                throws Exception {
922            final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
923            @SuppressWarnings("unused")
924            final SubscriptionInfo info = lookupSubscription(clientId, subscriptionName);
925            indexLock.writeLock().lock();
926            try {
927                pageFile.tx().execute(new Transaction.Closure<Exception>() {
928                    @Override
929                    public void execute(Transaction tx) throws Exception {
930                        StoredDestination sd = getStoredDestination(dest, tx);
931                        LastAck cursorPos = getLastAck(tx, sd, subscriptionKey);
932                        sd.orderIndex.setBatch(tx, cursorPos);
933                        recoverRolledBackAcks(sd, tx, Integer.MAX_VALUE, listener);
934                        for (Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx); iterator
935                                .hasNext();) {
936                            Entry<Long, MessageKeys> entry = iterator.next();
937                            if (ackedAndPrepared.contains(entry.getValue().messageId)) {
938                                continue;
939                            }
940                            listener.recoverMessage(loadMessage(entry.getValue().location));
941                        }
942                        sd.orderIndex.resetCursorPosition();
943                    }
944                });
945            } finally {
946                indexLock.writeLock().unlock();
947            }
948        }
949
950        @Override
951        public void recoverNextMessages(String clientId, String subscriptionName, final int maxReturned,
952                final MessageRecoveryListener listener) throws Exception {
953            final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
954            @SuppressWarnings("unused")
955            final SubscriptionInfo info = lookupSubscription(clientId, subscriptionName);
956            indexLock.writeLock().lock();
957            try {
958                pageFile.tx().execute(new Transaction.Closure<Exception>() {
959                    @Override
960                    public void execute(Transaction tx) throws Exception {
961                        StoredDestination sd = getStoredDestination(dest, tx);
962                        sd.orderIndex.resetCursorPosition();
963                        MessageOrderCursor moc = sd.subscriptionCursors.get(subscriptionKey);
964                        if (moc == null) {
965                            LastAck pos = getLastAck(tx, sd, subscriptionKey);
966                            if (pos == null) {
967                                // sub deleted
968                                return;
969                            }
970                            sd.orderIndex.setBatch(tx, pos);
971                            moc = sd.orderIndex.cursor;
972                        } else {
973                            sd.orderIndex.cursor.sync(moc);
974                        }
975
976                        Entry<Long, MessageKeys> entry = null;
977                        int counter = recoverRolledBackAcks(sd, tx, maxReturned, listener);
978                        for (Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx, moc); iterator
979                                .hasNext();) {
980                            entry = iterator.next();
981                            if (ackedAndPrepared.contains(entry.getValue().messageId)) {
982                                continue;
983                            }
984                            if (listener.recoverMessage(loadMessage(entry.getValue().location))) {
985                                counter++;
986                            }
987                            if (counter >= maxReturned || listener.hasSpace() == false) {
988                                break;
989                            }
990                        }
991                        sd.orderIndex.stoppedIterating();
992                        if (entry != null) {
993                            MessageOrderCursor copy = sd.orderIndex.cursor.copy();
994                            sd.subscriptionCursors.put(subscriptionKey, copy);
995                        }
996                    }
997                });
998            } finally {
999                indexLock.writeLock().unlock();
1000            }
1001        }
1002
1003        @Override
1004        public void resetBatching(String clientId, String subscriptionName) {
1005            try {
1006                final String subscriptionKey = subscriptionKey(clientId, subscriptionName);
1007                indexLock.writeLock().lock();
1008                try {
1009                    pageFile.tx().execute(new Transaction.Closure<IOException>() {
1010                        @Override
1011                        public void execute(Transaction tx) throws IOException {
1012                            StoredDestination sd = getStoredDestination(dest, tx);
1013                            sd.subscriptionCursors.remove(subscriptionKey);
1014                        }
1015                    });
1016                }finally {
1017                    indexLock.writeLock().unlock();
1018                }
1019            } catch (IOException e) {
1020                throw new RuntimeException(e);
1021            }
1022        }
1023    }
1024
1025    String subscriptionKey(String clientId, String subscriptionName) {
1026        return clientId + ":" + subscriptionName;
1027    }
1028
1029    @Override
1030    public MessageStore createQueueMessageStore(ActiveMQQueue destination) throws IOException {
1031        String key = key(convert(destination));
1032        MessageStore store = storeCache.get(key(convert(destination)));
1033        if (store == null) {
1034            final MessageStore queueStore = this.transactionStore.proxy(new KahaDBMessageStore(destination));
1035            store = storeCache.putIfAbsent(key, queueStore);
1036            if (store == null) {
1037                store = queueStore;
1038            }
1039        }
1040
1041        return store;
1042    }
1043
1044    @Override
1045    public TopicMessageStore createTopicMessageStore(ActiveMQTopic destination) throws IOException {
1046        String key = key(convert(destination));
1047        MessageStore store = storeCache.get(key(convert(destination)));
1048        if (store == null) {
1049            final TopicMessageStore topicStore = this.transactionStore.proxy(new KahaDBTopicMessageStore(destination));
1050            store = storeCache.putIfAbsent(key, topicStore);
1051            if (store == null) {
1052                store = topicStore;
1053            }
1054        }
1055
1056        return (TopicMessageStore) store;
1057    }
1058
1059    /**
1060     * Cleanup method to remove any state associated with the given destination.
1061     * This method does not stop the message store (it might not be cached).
1062     *
1063     * @param destination
1064     *            Destination to forget
1065     */
1066    @Override
1067    public void removeQueueMessageStore(ActiveMQQueue destination) {
1068    }
1069
1070    /**
1071     * Cleanup method to remove any state associated with the given destination
1072     * This method does not stop the message store (it might not be cached).
1073     *
1074     * @param destination
1075     *            Destination to forget
1076     */
1077    @Override
1078    public void removeTopicMessageStore(ActiveMQTopic destination) {
1079    }
1080
1081    @Override
1082    public void deleteAllMessages() throws IOException {
1083        deleteAllMessages = true;
1084    }
1085
1086    @Override
1087    public Set<ActiveMQDestination> getDestinations() {
1088        try {
1089            final HashSet<ActiveMQDestination> rc = new HashSet<ActiveMQDestination>();
1090            indexLock.writeLock().lock();
1091            try {
1092                pageFile.tx().execute(new Transaction.Closure<IOException>() {
1093                    @Override
1094                    public void execute(Transaction tx) throws IOException {
1095                        for (Iterator<Entry<String, StoredDestination>> iterator = metadata.destinations.iterator(tx); iterator
1096                                .hasNext();) {
1097                            Entry<String, StoredDestination> entry = iterator.next();
1098                            //Removing isEmpty topic check - see AMQ-5875
1099                            rc.add(convert(entry.getKey()));
1100                        }
1101                    }
1102                });
1103            }finally {
1104                indexLock.writeLock().unlock();
1105            }
1106            return rc;
1107        } catch (IOException e) {
1108            throw new RuntimeException(e);
1109        }
1110    }
1111
1112    @Override
1113    public long getLastMessageBrokerSequenceId() throws IOException {
1114        return 0;
1115    }
1116
1117    @Override
1118    public long getLastProducerSequenceId(ProducerId id) {
1119        indexLock.writeLock().lock();
1120        try {
1121            return metadata.producerSequenceIdTracker.getLastSeqId(id);
1122        } finally {
1123            indexLock.writeLock().unlock();
1124        }
1125    }
1126
1127    @Override
1128    public long size() {
1129        try {
1130            return journalSize.get() + getPageFile().getDiskSize();
1131        } catch (IOException e) {
1132            throw new RuntimeException(e);
1133        }
1134    }
1135
1136    @Override
1137    public void beginTransaction(ConnectionContext context) throws IOException {
1138        throw new IOException("Not yet implemented.");
1139    }
1140    @Override
1141    public void commitTransaction(ConnectionContext context) throws IOException {
1142        throw new IOException("Not yet implemented.");
1143    }
1144    @Override
1145    public void rollbackTransaction(ConnectionContext context) throws IOException {
1146        throw new IOException("Not yet implemented.");
1147    }
1148
1149    @Override
1150    public void checkpoint(boolean sync) throws IOException {
1151        super.checkpointCleanup(sync);
1152    }
1153
1154    // /////////////////////////////////////////////////////////////////
1155    // Internal helper methods.
1156    // /////////////////////////////////////////////////////////////////
1157
1158    /**
1159     * @param location
1160     * @return
1161     * @throws IOException
1162     */
1163    Message loadMessage(Location location) throws IOException {
1164        JournalCommand<?> command = load(location);
1165        KahaAddMessageCommand addMessage = null;
1166        switch (command.type()) {
1167            case KAHA_UPDATE_MESSAGE_COMMAND:
1168                addMessage = ((KahaUpdateMessageCommand)command).getMessage();
1169                break;
1170            default:
1171                addMessage = (KahaAddMessageCommand) command;
1172        }
1173        Message msg = (Message) wireFormat.unmarshal(new DataInputStream(addMessage.getMessage().newInput()));
1174        return msg;
1175    }
1176
1177    // /////////////////////////////////////////////////////////////////
1178    // Internal conversion methods.
1179    // /////////////////////////////////////////////////////////////////
1180
1181    KahaLocation convert(Location location) {
1182        KahaLocation rc = new KahaLocation();
1183        rc.setLogId(location.getDataFileId());
1184        rc.setOffset(location.getOffset());
1185        return rc;
1186    }
1187
1188    KahaDestination convert(ActiveMQDestination dest) {
1189        KahaDestination rc = new KahaDestination();
1190        rc.setName(dest.getPhysicalName());
1191        switch (dest.getDestinationType()) {
1192        case ActiveMQDestination.QUEUE_TYPE:
1193            rc.setType(DestinationType.QUEUE);
1194            return rc;
1195        case ActiveMQDestination.TOPIC_TYPE:
1196            rc.setType(DestinationType.TOPIC);
1197            return rc;
1198        case ActiveMQDestination.TEMP_QUEUE_TYPE:
1199            rc.setType(DestinationType.TEMP_QUEUE);
1200            return rc;
1201        case ActiveMQDestination.TEMP_TOPIC_TYPE:
1202            rc.setType(DestinationType.TEMP_TOPIC);
1203            return rc;
1204        default:
1205            return null;
1206        }
1207    }
1208
1209    ActiveMQDestination convert(String dest) {
1210        int p = dest.indexOf(":");
1211        if (p < 0) {
1212            throw new IllegalArgumentException("Not in the valid destination format");
1213        }
1214        int type = Integer.parseInt(dest.substring(0, p));
1215        String name = dest.substring(p + 1);
1216        return convert(type, name);
1217    }
1218
1219    private ActiveMQDestination convert(KahaDestination commandDestination) {
1220        return convert(commandDestination.getType().getNumber(), commandDestination.getName());
1221    }
1222
1223    private ActiveMQDestination convert(int type, String name) {
1224        switch (KahaDestination.DestinationType.valueOf(type)) {
1225        case QUEUE:
1226            return new ActiveMQQueue(name);
1227        case TOPIC:
1228            return new ActiveMQTopic(name);
1229        case TEMP_QUEUE:
1230            return new ActiveMQTempQueue(name);
1231        case TEMP_TOPIC:
1232            return new ActiveMQTempTopic(name);
1233        default:
1234            throw new IllegalArgumentException("Not in the valid destination format");
1235        }
1236    }
1237
1238    public TransactionIdTransformer getTransactionIdTransformer() {
1239        return transactionIdTransformer;
1240    }
1241
1242    public void setTransactionIdTransformer(TransactionIdTransformer transactionIdTransformer) {
1243        this.transactionIdTransformer = transactionIdTransformer;
1244    }
1245
1246    static class AsyncJobKey {
1247        MessageId id;
1248        ActiveMQDestination destination;
1249
1250        AsyncJobKey(MessageId id, ActiveMQDestination destination) {
1251            this.id = id;
1252            this.destination = destination;
1253        }
1254
1255        @Override
1256        public boolean equals(Object obj) {
1257            if (obj == this) {
1258                return true;
1259            }
1260            return obj instanceof AsyncJobKey && id.equals(((AsyncJobKey) obj).id)
1261                    && destination.equals(((AsyncJobKey) obj).destination);
1262        }
1263
1264        @Override
1265        public int hashCode() {
1266            return id.hashCode() + destination.hashCode();
1267        }
1268
1269        @Override
1270        public String toString() {
1271            return destination.getPhysicalName() + "-" + id;
1272        }
1273    }
1274
1275    public interface StoreTask {
1276        public boolean cancel();
1277
1278        public void aquireLocks();
1279
1280        public void releaseLocks();
1281    }
1282
1283    class StoreQueueTask implements Runnable, StoreTask {
1284        protected final Message message;
1285        protected final ConnectionContext context;
1286        protected final KahaDBMessageStore store;
1287        protected final InnerFutureTask future;
1288        protected final AtomicBoolean done = new AtomicBoolean();
1289        protected final AtomicBoolean locked = new AtomicBoolean();
1290
1291        public StoreQueueTask(KahaDBMessageStore store, ConnectionContext context, Message message) {
1292            this.store = store;
1293            this.context = context;
1294            this.message = message;
1295            this.future = new InnerFutureTask(this);
1296        }
1297
1298        public ListenableFuture<Object> getFuture() {
1299            return this.future;
1300        }
1301
1302        @Override
1303        public boolean cancel() {
1304            if (this.done.compareAndSet(false, true)) {
1305                return this.future.cancel(false);
1306            }
1307            return false;
1308        }
1309
1310        @Override
1311        public void aquireLocks() {
1312            if (this.locked.compareAndSet(false, true)) {
1313                try {
1314                    globalQueueSemaphore.acquire();
1315                    store.acquireLocalAsyncLock();
1316                    message.incrementReferenceCount();
1317                } catch (InterruptedException e) {
1318                    LOG.warn("Failed to aquire lock", e);
1319                }
1320            }
1321
1322        }
1323
1324        @Override
1325        public void releaseLocks() {
1326            if (this.locked.compareAndSet(true, false)) {
1327                store.releaseLocalAsyncLock();
1328                globalQueueSemaphore.release();
1329                message.decrementReferenceCount();
1330            }
1331        }
1332
1333        @Override
1334        public void run() {
1335            this.store.doneTasks++;
1336            try {
1337                if (this.done.compareAndSet(false, true)) {
1338                    this.store.addMessage(context, message);
1339                    removeQueueTask(this.store, this.message.getMessageId());
1340                    this.future.complete();
1341                } else if (cancelledTaskModMetric > 0 && this.store.canceledTasks++ % cancelledTaskModMetric == 0) {
1342                    System.err.println(this.store.dest.getName() + " cancelled: "
1343                            + (this.store.canceledTasks / this.store.doneTasks) * 100);
1344                    this.store.canceledTasks = this.store.doneTasks = 0;
1345                }
1346            } catch (Exception e) {
1347                this.future.setException(e);
1348            }
1349        }
1350
1351        protected Message getMessage() {
1352            return this.message;
1353        }
1354
1355        private class InnerFutureTask extends FutureTask<Object> implements ListenableFuture<Object>  {
1356
1357            private Runnable listener;
1358            public InnerFutureTask(Runnable runnable) {
1359                super(runnable, null);
1360
1361            }
1362
1363            public void setException(final Exception e) {
1364                super.setException(e);
1365            }
1366
1367            public void complete() {
1368                super.set(null);
1369            }
1370
1371            @Override
1372            public void done() {
1373                fireListener();
1374            }
1375
1376            @Override
1377            public void addListener(Runnable listener) {
1378                this.listener = listener;
1379                if (isDone()) {
1380                    fireListener();
1381                }
1382            }
1383
1384            private void fireListener() {
1385                if (listener != null) {
1386                    try {
1387                        listener.run();
1388                    } catch (Exception ignored) {
1389                        LOG.warn("Unexpected exception from future {} listener callback {}", this, listener, ignored);
1390                    }
1391                }
1392            }
1393        }
1394    }
1395
1396    class StoreTopicTask extends StoreQueueTask {
1397        private final int subscriptionCount;
1398        private final List<String> subscriptionKeys = new ArrayList<String>(1);
1399        private final KahaDBTopicMessageStore topicStore;
1400        public StoreTopicTask(KahaDBTopicMessageStore store, ConnectionContext context, Message message,
1401                int subscriptionCount) {
1402            super(store, context, message);
1403            this.topicStore = store;
1404            this.subscriptionCount = subscriptionCount;
1405
1406        }
1407
1408        @Override
1409        public void aquireLocks() {
1410            if (this.locked.compareAndSet(false, true)) {
1411                try {
1412                    globalTopicSemaphore.acquire();
1413                    store.acquireLocalAsyncLock();
1414                    message.incrementReferenceCount();
1415                } catch (InterruptedException e) {
1416                    LOG.warn("Failed to aquire lock", e);
1417                }
1418            }
1419        }
1420
1421        @Override
1422        public void releaseLocks() {
1423            if (this.locked.compareAndSet(true, false)) {
1424                message.decrementReferenceCount();
1425                store.releaseLocalAsyncLock();
1426                globalTopicSemaphore.release();
1427            }
1428        }
1429
1430        /**
1431         * add a key
1432         *
1433         * @param key
1434         * @return true if all acknowledgements received
1435         */
1436        public boolean addSubscriptionKey(String key) {
1437            synchronized (this.subscriptionKeys) {
1438                this.subscriptionKeys.add(key);
1439            }
1440            return this.subscriptionKeys.size() >= this.subscriptionCount;
1441        }
1442
1443        @Override
1444        public void run() {
1445            this.store.doneTasks++;
1446            try {
1447                if (this.done.compareAndSet(false, true)) {
1448                    this.topicStore.addMessage(context, message);
1449                    // apply any acks we have
1450                    synchronized (this.subscriptionKeys) {
1451                        for (String key : this.subscriptionKeys) {
1452                            this.topicStore.doAcknowledge(context, key, this.message.getMessageId(), null);
1453
1454                        }
1455                    }
1456                    removeTopicTask(this.topicStore, this.message.getMessageId());
1457                    this.future.complete();
1458                } else if (cancelledTaskModMetric > 0 && this.store.canceledTasks++ % cancelledTaskModMetric == 0) {
1459                    System.err.println(this.store.dest.getName() + " cancelled: "
1460                            + (this.store.canceledTasks / this.store.doneTasks) * 100);
1461                    this.store.canceledTasks = this.store.doneTasks = 0;
1462                }
1463            } catch (Exception e) {
1464                this.future.setException(e);
1465            }
1466        }
1467    }
1468
1469    public class StoreTaskExecutor extends ThreadPoolExecutor {
1470
1471        public StoreTaskExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit timeUnit, BlockingQueue<Runnable> queue, ThreadFactory threadFactory) {
1472            super(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, queue, threadFactory);
1473        }
1474
1475        @Override
1476        protected void afterExecute(Runnable runnable, Throwable throwable) {
1477            super.afterExecute(runnable, throwable);
1478
1479            if (runnable instanceof StoreTask) {
1480               ((StoreTask)runnable).releaseLocks();
1481            }
1482        }
1483    }
1484
1485    @Override
1486    public JobSchedulerStore createJobSchedulerStore() throws IOException, UnsupportedOperationException {
1487        return new JobSchedulerStoreImpl();
1488    }
1489}