// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.dns;

import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.hosted.controller.api.integration.dns.AliasTarget;
import com.yahoo.vespa.hosted.controller.api.integration.dns.NameService;
import com.yahoo.vespa.hosted.controller.api.integration.dns.Record;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordData;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordName;
import com.yahoo.vespa.hosted.controller.maintenance.NameServiceDispatcher;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * This adds name service requests to the {@link NameServiceQueue}.
 *
 * Name service requests passed to this are not immediately sent to a name service, but are instead persisted
 * in a curator-backed queue. Enqueued requests are later dispatched to a {@link NameService} by
 * {@link NameServiceDispatcher}.
 *
 * @author mpolden
 */
public class NameServiceForwarder {

    private static final int maxQueuedRequests = 200;

    private static final Logger log = Logger.getLogger(NameServiceForwarder.class.getName());

    private final CuratorDb db;

    public NameServiceForwarder(CuratorDb db) {
        this.db = Objects.requireNonNull(db, "db must be non-null");
    }

    /** Create or update a CNAME record with given name and data */
    public void createCname(RecordName name, RecordData canonicalName, NameServiceQueue.Priority priority) {
        forward(new CreateRecord(new Record(Record.Type.CNAME, name, canonicalName)), priority);
    }

    /** Create or update an ALIAS record with given name and targets */
    public void createAlias(RecordName name, Set<AliasTarget> targets, NameServiceQueue.Priority priority) {
        var records = targets.stream()
                             .map(target -> new Record(Record.Type.ALIAS, name, target.pack()))
                             .collect(Collectors.toList());
        forward(new CreateRecords(records), priority);
    }

    /** Create or update a TXT record with given name and data */
    public void createTxt(RecordName name, List<RecordData> txtData, NameServiceQueue.Priority priority) {
        var records = txtData.stream()
                             .map(data -> new Record(Record.Type.TXT, name, data))
                             .collect(Collectors.toList());
        forward(new CreateRecords(records), priority);
    }

    /** Remove all records of given type and name */
    public void removeRecords(Record.Type type, RecordName name, NameServiceQueue.Priority priority) {
        forward(new RemoveRecords(type, name), priority);
    }

    /** Remove all records of given type and data */
    public void removeRecords(Record.Type type, RecordData data, NameServiceQueue.Priority priority) {
        forward(new RemoveRecords(type, data), priority);
    }

    protected void forward(NameServiceRequest request, NameServiceQueue.Priority priority) {
        try (Lock lock = db.lockNameServiceQueue()) {
            NameServiceQueue queue = db.readNameServiceQueue();
            var queued = queue.requests().size();
            if (queued >= maxQueuedRequests) {
                log.log(Level.WARNING, "Queue is at capacity (size: " + queued + "), dropping older " +
                                          "requests. This likely means that the name service is not successfully " +
                                          "executing requests");
            }
            log.log(Level.FINE, () -> "Queueing name service request: " + request);
            db.writeNameServiceQueue(queue.with(request, priority).last(maxQueuedRequests));
        }
    }

}

@@ -1280,29 +1280,26 @@
 int 
-maxQueuedRequests = 2
+QUEUE_CAPACITY = 3
 00;%0A
@@ -3416,33 +3416,30 @@
 eued %3E= 
-maxQueuedRequests
+QUEUE_CAPACITY
 ) %7B%0A    
@@ -3894,25 +3894,22 @@
 ast(
-maxQueuedRequests
+QUEUE_CAPACITY
 ));%0A

Bump capacity of DNS request queue (#18090)