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 */
019
020package org.nuxeo.importer.stream.producer;
021
022import java.time.Duration;
023
024import org.nuxeo.ecm.core.blob.BlobInfo;
025import org.nuxeo.importer.stream.message.BlobInfoMessage;
026import org.nuxeo.importer.stream.message.DocumentMessage;
027import org.nuxeo.lib.stream.log.LogRecord;
028import org.nuxeo.lib.stream.log.LogTailer;
029
030/**
031 * Returns blob information from a Log, loop on the log.
032 *
033 * @since 9.3
034 */
035public class RandomLogBlobInfoFetcher implements BlobInfoFetcher {
036    protected static final Duration READ_DELAY = Duration.ofMillis(100);
037
038    protected static final Duration FIRST_READ_DELAY = Duration.ofSeconds(1);
039
040    protected final LogTailer<BlobInfoMessage> tailer;
041
042    protected boolean first;
043
044    public RandomLogBlobInfoFetcher(LogTailer<BlobInfoMessage> blobInfoTailer) {
045        this.tailer = blobInfoTailer;
046        this.first = true;
047    }
048
049    @Override
050    public BlobInfo get(DocumentMessage.Builder builder) {
051        LogRecord<BlobInfoMessage> record;
052        try {
053            record = tailer.read(getDuration());
054        } catch (InterruptedException e) {
055            Thread.currentThread().interrupt();
056            throw new RuntimeException(e);
057        }
058        if (record == null) {
059            if (first) {
060                // there is no record in this partition, no need to loop
061                return null;
062            }
063            // start again from beginning
064            first = true;
065            tailer.toStart();
066            return get(builder);
067        }
068        first = false;
069        return record.message();
070    }
071
072    protected Duration getDuration() {
073        if (first) {
074            // first read may need more time on slow infra
075            return FIRST_READ_DELAY;
076        }
077        return READ_DELAY;
078    }
079
080    @Override
081    public void close() {
082        tailer.close();
083    }
084}