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

org.apache.kafka.connect.runtime.WorkerConfigTransformer Maven / Gradle / Ivy

There is a newer version: 3.7.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.kafka.connect.runtime;

import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.provider.ConfigProvider;
import org.apache.kafka.common.config.ConfigTransformer;
import org.apache.kafka.common.config.ConfigTransformerResult;
import org.apache.kafka.connect.runtime.Herder.ConfigReloadAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * A wrapper class to perform configuration transformations and schedule reloads for any
 * retrieved TTL values.
 */
public class WorkerConfigTransformer {
    private static final Logger log = LoggerFactory.getLogger(WorkerConfigTransformer.class);

    private final Worker worker;
    private final ConfigTransformer configTransformer;
    private final ConcurrentMap> requests = new ConcurrentHashMap<>();

    public WorkerConfigTransformer(Worker worker, Map configProviders) {
        this.worker = worker;
        this.configTransformer = new ConfigTransformer(configProviders);
    }

    public Map transform(Map configs) {
        return transform(null, configs);
    }

    public Map transform(String connectorName, Map configs) {
        if (configs == null) return null;
        ConfigTransformerResult result = configTransformer.transform(configs);
        if (connectorName != null) {
            String key = ConnectorConfig.CONFIG_RELOAD_ACTION_CONFIG;
            String action = (String) ConfigDef.parseType(key, configs.get(key), ConfigDef.Type.STRING);
            if (action == null) {
                // The default action is "restart".
                action = ConnectorConfig.CONFIG_RELOAD_ACTION_RESTART;
            }
            ConfigReloadAction reloadAction = ConfigReloadAction.valueOf(action.toUpperCase(Locale.ROOT));
            if (reloadAction == ConfigReloadAction.RESTART) {
                scheduleReload(connectorName, result.ttls());
            }
        }
        return result.data();
    }

    private void scheduleReload(String connectorName, Map ttls) {
        for (Map.Entry entry : ttls.entrySet()) {
            scheduleReload(connectorName, entry.getKey(), entry.getValue());
        }
    }

    private void scheduleReload(String connectorName, String path, long ttl) {
        Map connectorRequests = requests.get(connectorName);
        if (connectorRequests == null) {
            connectorRequests = new ConcurrentHashMap<>();
            requests.put(connectorName, connectorRequests);
        } else {
            HerderRequest previousRequest = connectorRequests.get(path);
            if (previousRequest != null) {
                // Delete previous request for ttl which is now stale
                previousRequest.cancel();
            }
        }
        log.info("Scheduling a restart of connector {} in {} ms", connectorName, ttl);
        HerderRequest request = worker.herder().restartConnector(ttl, connectorName, null);
        connectorRequests.put(path, request);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy