All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vmware.xenon.common.OperationTracker Maven / Gradle / Ivy

There is a newer version: 1.6.18
Show newest version
/*
 * Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.vmware.xenon.common;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;

import com.vmware.xenon.common.Service.ProcessingStage;

/**
 * Performs periodic maintenance and expiration tracking on operations. Utilized by
 * service host for all operation related maintenance.
 */
class OperationTracker {
    public static ConcurrentSkipListSet createOperationSet() {
        return new ConcurrentSkipListSet<>(new Comparator() {
            @Override
            public int compare(Operation o1, Operation o2) {
                return Long.compare(o1.getId(),
                        o2.getId());
            }
        });
    }

    private ServiceHost host;
    private final SortedSet pendingStartOperations = createOperationSet();
    private final ConcurrentHashMap> pendingServiceAvailableCompletions = new ConcurrentHashMap<>();
    private final ConcurrentHashMap pendingOperationsForRetry = new ConcurrentHashMap<>();

    public static OperationTracker create(ServiceHost host) {
        OperationTracker omt = new OperationTracker();
        omt.host = host;
        return omt;
    }

    public void trackOperationForRetry(long expirationMicros, Throwable e, Operation op) {
        this.host.log(Level.WARNING,
                "Retrying id %d to %s (retries: %d). Failure: %s",
                op.getId(), op.getUri(),
                op.getRetryCount(),
                e.toString());
        op.incrementRetryCount();
        this.pendingOperationsForRetry.put(expirationMicros, op);
    }

    public void trackStartOperation(Operation op) {
        this.pendingStartOperations.add(op);
    }

    public void removeStartOperation(Operation post) {
        this.pendingStartOperations.remove(post);
    }

    public SortedSet trackServiceAvailableCompletion(String link,
            Operation opTemplate, boolean doOpClone) {
        SortedSet pendingOps = this.pendingServiceAvailableCompletions
                .computeIfAbsent(link, (k) -> {
                    return createOperationSet();
                });
        pendingOps.add(doOpClone ? opTemplate.clone() : opTemplate);
        return pendingOps;
    }

    public boolean hasPendingServiceAvailableCompletions(String link) {
        return this.pendingServiceAvailableCompletions.containsKey(link);
    }

    public SortedSet removeServiceAvailableCompletions(String link) {
        return this.pendingServiceAvailableCompletions.remove(link);
    }

    public void performMaintenance(long nowMicros) {
        Iterator startOpsIt = this.pendingStartOperations.iterator();
        checkOperationExpiration(nowMicros, startOpsIt);

        for (Entry> entry : this.pendingServiceAvailableCompletions
                .entrySet()) {
            String link = entry.getKey();
            SortedSet pendingOps = entry.getValue();
            Service s = this.host.findService(link, true);
            if (s != null && s.getProcessingStage() == ProcessingStage.AVAILABLE) {
                this.host.log(Level.WARNING,
                        "Service %s available, but has pending start operations", link);
                this.host.processPendingServiceAvailableOperations(s, null, false);
                break;
            }
            Iterator it = pendingOps.iterator();
            checkOperationExpiration(nowMicros, it);
        }

        final long intervalMicros = TimeUnit.SECONDS.toMicros(1);
        Iterator> it = this.pendingOperationsForRetry.entrySet().iterator();
        while (it.hasNext()) {
            Entry entry = it.next();
            Operation o = entry.getValue();
            if (this.host.isStopping()) {
                o.fail(new CancellationException());
                return;
            }

            // Apply linear back-off: we delay retry based on the number of retry attempts. We
            // keep retrying until expiration of the operation (applied in retryOrFailRequest)
            long queuingTimeMicros = entry.getKey();
            long estimatedRetryTimeMicros = o.getRetryCount() * intervalMicros +
                    queuingTimeMicros;
            if (estimatedRetryTimeMicros > nowMicros) {
                continue;
            }
            it.remove();
            this.host.handleRequest(null, o);
        }
    }

    private void checkOperationExpiration(long now, Iterator iterator) {
        while (iterator.hasNext()) {
            Operation op = iterator.next();
            if (op == null || op.getExpirationMicrosUtc() > now) {
                // not expired, and since we walk in ascending order, no other operations
                // are expired
                break;
            }
            iterator.remove();
            this.host.run(() -> op.fail(new TimeoutException(op.toString())));
        }
    }

    public void close() {
        for (Operation op : this.pendingOperationsForRetry.values()) {
            op.fail(new CancellationException());
        }
        this.pendingOperationsForRetry.clear();

        for (Operation op : this.pendingStartOperations) {
            op.fail(new CancellationException());
        }
        this.pendingStartOperations.clear();

        for (SortedSet opSet : this.pendingServiceAvailableCompletions.values()) {
            for (Operation op : opSet) {
                op.fail(new CancellationException());
            }
        }
        this.pendingServiceAvailableCompletions.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy