001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.log;
020
021import java.io.Externalizable;
022import java.time.Duration;
023import java.util.Objects;
024
025import org.nuxeo.lib.stream.codec.Codec;
026
027/**
028 * An appender is used to append message into a Log. Implementations must be thread safe.
029 *
030 * @since 9.3
031 */
032public interface LogAppender<M extends Externalizable> {
033
034    /**
035     * Returns the Log's name.
036     */
037    Name name();
038
039    /**
040     * Returns the number of partitions in the Log.
041     */
042    int size();
043
044    /**
045     * Append a message into a partition, returns {@link LogOffset} position of the message. This method is thread safe,
046     * a queue can be shared by multiple producers.
047     *
048     * @param partition index lower than {@link #size()}
049     */
050    LogOffset append(int partition, M message);
051
052    /**
053     * Same as {@link #append(int, Externalizable)}, the queue is chosen using a hash of parameters "key".
054     */
055    default LogOffset append(String key, M message) {
056        Objects.requireNonNull(key);
057        // Provide a basic partitioning that works because:
058        // 1. String.hashCode is known to be constant even with different JVM (this is not the case for all objects)
059        // 2. the modulo operator is not optimal when rebalancing on partitions resizing but this should not happen.
060        // and yes hashCode can be negative.
061        int partition = (key.hashCode() & 0x7fffffff) % size();
062        return append(partition, message);
063    }
064
065    /**
066     * Wait for consumer to process a message up to the offset. The message is processed if a consumer of the group
067     * commits a greater or equals offset. Return {@code true} if the message has been consumed, {@code false} in case
068     * of timeout.
069     */
070    boolean waitFor(LogOffset offset, Name group, Duration timeout) throws InterruptedException;
071
072    /**
073     * Returns {@code true} if the appender has been closed by the manager.
074     */
075    boolean closed();
076
077    /**
078     * Returns the codec used to write record. A null codec is the default legacy encoding.
079     *
080     * @since 10.2
081     */
082    Codec<M> getCodec();
083}