org.microbean.kubernetes.controller.EventDistributor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of microbean-kubernetes-controller Show documentation
Show all versions of microbean-kubernetes-controller Show documentation
Tools for writing Kubernetes controllers in Java.
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2017-2018 microBean.
*
* 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 org.microbean.kubernetes.controller;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import io.fabric8.kubernetes.api.model.HasMetadata;
import net.jcip.annotations.Immutable;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;
/**
* A {@link ResourceTrackingEventQueueConsumer} that {@linkplain
* ResourceTrackingEventQueueConsumer#accept(EventQueue) consumes
* EventQueue instances} by feeding each {@link
* AbstractEvent} in the {@link EventQueue} being consumed to {@link
* Consumer}s of {@link AbstractEvent}s that have been {@linkplain
* #addConsumer(Consumer) registered}.
*
* {@link EventDistributor} instances must be {@linkplain #close()
* closed} and discarded after use.
*
* @param a type of Kubernetes resource
*
* @author Laird Nelson
*
* @see #addConsumer(Consumer)
*
* @see #removeConsumer(Consumer)
*/
@Immutable
@ThreadSafe
public final class EventDistributor extends ResourceTrackingEventQueueConsumer implements AutoCloseable {
/*
* Instance fields.
*/
@GuardedBy("readLock && writeLock")
private final Collection> distributors;
@GuardedBy("readLock && writeLock")
private final Collection> synchronizingDistributors;
private final Duration synchronizationInterval;
private final Lock readLock;
private final Lock writeLock;
/*
* Constructors.
*/
/**
* Creates a new {@link EventDistributor}.
*
* @param knownObjects a mutable {@link Map} of Kubernetes resources
* that contains or will contain Kubernetes resources known to this
* {@link EventDistributor} and whatever mechanism (such as a {@link
* Controller}) is feeding it; may be {@code null}
*
* @see
* ResourceTrackingEventQueueConsumer#ResourceTrackingEventQueueConsumer(Map)
*/
public EventDistributor(final Map