Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimic storage notifications using the HTTP API #1339

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
<dependency>
<groupId>io.k8ssandra</groupId>
<artifactId>datastax-mgmtapi-client-openapi</artifactId>
<version>0.1.0-c22a2fc</version>
<version>0.1.0-4d2a772</version>
</dependency>
<!--test scope -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void run(ReaperApplicationConfiguration config, Environment environment)
Cryptograph cryptograph = context.config == null || context.config.getCryptograph() == null
? new NoopCrypotograph() : context.config.getCryptograph().create();

initializeManagement(context, cryptograph);
initializeManagement(context, environment, cryptograph);

context.repairManager = RepairManager.create(
context,
Expand Down Expand Up @@ -299,14 +299,17 @@ public void run(ReaperApplicationConfiguration config, Environment environment)
}


private void initializeManagement(AppContext context, Cryptograph cryptograph) {
private void initializeManagement(AppContext context, Environment environment, Cryptograph cryptograph) {
if (context.managementConnectionFactory == null) {
LOG.info("no management connection factory given in context, creating default");
if (context.config.getHttpManagement() == null || !context.config.getHttpManagement().isEnabled()) {
LOG.info("HTTP management connection config not set, or set disabled. Creating JMX connection factory instead");
context.managementConnectionFactory = new JmxManagementConnectionFactory(context, cryptograph);
} else {
context.managementConnectionFactory = new HttpManagementConnectionFactory(context);
ScheduledExecutorService jobStatusPollerExecutor = environment.lifecycle()
.scheduledExecutorService("JobStatusPoller")
.threads(2).build();
context.managementConnectionFactory = new HttpManagementConnectionFactory(context, jobStatusPollerExecutor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 The Last Pickle Ltd
* Copyright 2023-2023 DataStax, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I think this should be

 * Copyright 2020-2022 The Last Pickle Ltd
 * Copyright 2023 DataStax, Inc.

But I'm not a lawyer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkstyle won't accept that format. In any case, this type of copyright info is not necessary in EU at least.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same in AU, but we need to consider US.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, then that would be outside PR which would modify the checkstyle and copyright creation in the project. Right now the years are all over the place in every file.

*
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,6 +22,7 @@
import io.cassandrareaper.core.Table;
import io.cassandrareaper.management.ICassandraManagementProxy;
import io.cassandrareaper.management.RepairStatusHandler;
import io.cassandrareaper.management.http.models.JobStatusTracker;
import io.cassandrareaper.service.RingRange;

import java.io.IOException;
Expand All @@ -35,42 +36,66 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.management.JMException;
import javax.management.openmbean.CompositeData;
import javax.validation.constraints.NotNull;

import com.codahale.metrics.MetricRegistry;
import com.datastax.mgmtapi.client.api.DefaultApi;
import com.datastax.mgmtapi.client.invoker.ApiClient;
import com.datastax.mgmtapi.client.invoker.ApiException;
import com.datastax.mgmtapi.client.model.EndpointStates;
import com.datastax.mgmtapi.client.model.Job;
import com.datastax.mgmtapi.client.model.RepairRequest;
import com.datastax.mgmtapi.client.model.SnapshotDetails;
import com.datastax.mgmtapi.client.model.StatusChange;
import com.datastax.mgmtapi.client.model.TakeSnapshotRequest;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import org.apache.cassandra.repair.RepairParallelism;
import org.apache.cassandra.utils.progress.ProgressEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpCassandraManagementProxy implements ICassandraManagementProxy {

public static final int DEFAULT_POLL_INTERVAL_IN_MILLISECONDS = 5000;
burmanm marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger LOG = LoggerFactory.getLogger(HttpCassandraManagementProxy.class);
final String host;
final MetricRegistry metricRegistry;
final String rootPath;
final InetSocketAddress endpoint;
final DefaultApi apiClient;

final ConcurrentMap<Integer, RepairStatusHandler> repairStatusHandlers = Maps.newConcurrentMap();
final ConcurrentMap<String, JobStatusTracker> jobTracker = Maps.newConcurrentMap();
final ConcurrentMap<Integer, ExecutorService> repairStatusExecutors = Maps.newConcurrentMap();


private ScheduledExecutorService statusTracker;

public HttpCassandraManagementProxy(MetricRegistry metricRegistry,
String rootPath,
InetSocketAddress endpoint
InetSocketAddress endpoint,
ScheduledExecutorService executor,
DefaultApi apiClient
) {
this.host = endpoint.getHostString();
this.metricRegistry = metricRegistry;
this.rootPath = rootPath;
this.endpoint = endpoint;
this.apiClient = new DefaultApi(
new ApiClient().setBasePath("http://" + endpoint.getHostName() + ":" + endpoint.getPort() + rootPath));
this.apiClient = apiClient;
this.statusTracker = executor;

// TODO Perhaps the poll interval should be configurable through context.config ?
burmanm marked this conversation as resolved.
Show resolved Hide resolved
this.scheduleJobPoller(statusTracker, DEFAULT_POLL_INTERVAL_IN_MILLISECONDS);
burmanm marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down Expand Up @@ -192,13 +217,31 @@ public int triggerRepair(
List<RingRange> associatedTokens,
int repairThreadCount)
throws ReaperException {
return 1; //TODO: implement me

String jobId;
Miles-Garnsey marked this conversation as resolved.
Show resolved Hide resolved
try {
jobId = apiClient.repair1(new RepairRequest());
} catch (ApiException e) {
throw new ReaperException(e);
}

int repairNo = Integer.parseInt(jobId.substring(7));

repairStatusExecutors.putIfAbsent(repairNo, Executors.newSingleThreadExecutor());
burmanm marked this conversation as resolved.
Show resolved Hide resolved
repairStatusHandlers.putIfAbsent(repairNo, repairStatusHandler);
jobTracker.put(jobId, new JobStatusTracker());
return repairNo;
}

@Override
public void removeRepairStatusHandler(int repairNo) {
// TODO: implement me.
repairStatusHandlers.remove(repairNo);
ExecutorService repairStatusExecutor = repairStatusExecutors.remove(repairNo);
if (null != repairStatusExecutor) {
repairStatusExecutor.shutdown();
}
String jobId = String.format("repair-%d", repairNo);
jobTracker.remove(jobId);
}

@Override
Expand Down Expand Up @@ -348,4 +391,54 @@ public String getUntranslatedHost() {
//TODO: implement me
return "";
}

private Job getJobStatus(String id) {
// Poll with HTTP client the job's status
try {
return apiClient.getJobStatus(id);
} catch (ApiException e) {
throw new RuntimeException(e);
}
}

@VisibleForTesting
private void scheduleJobPoller(ScheduledExecutorService scheduler, int pollInterval) {
burmanm marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this ScheduledExecutorService argument and call this.scheduler?

scheduler.scheduleWithFixedDelay(
notificationsTracker(),
pollInterval * 2,
pollInterval,
TimeUnit.MILLISECONDS);
}

@VisibleForTesting
Runnable notificationsTracker() {
return () -> {
if (jobTracker.size() > 0) {
for (Map.Entry<String, JobStatusTracker> entry : jobTracker.entrySet()) {
Job job = getJobStatus(entry.getKey());
int availableNotifications = job.getStatusChanges().size();
int currentNotificationCount = entry.getValue().latestNotificationCount.get();

if (currentNotificationCount < availableNotifications) {
// We need to process the new ones
for (int i = currentNotificationCount; i < availableNotifications; i++) {
StatusChange statusChange = job.getStatusChanges().get(i);
// remove "repair-" prefix
int repairNo = Integer.parseInt(job.getId().substring(7));
ProgressEventType progressType = ProgressEventType.valueOf(statusChange.getStatus());
repairStatusExecutors.get(repairNo).submit(() -> {
repairStatusHandlers
.get(repairNo)
.handle(repairNo, Optional.empty(), Optional.of(progressType),
statusChange.getMessage(), this);
});

// Update the count as we process them
entry.getValue().latestNotificationCount.incrementAndGet();
}
}
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledExecutorService;
import javax.ws.rs.core.Response;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.InstrumentedScheduledExecutorService;
import com.codahale.metrics.MetricRegistry;
import com.datastax.mgmtapi.client.api.DefaultApi;
import com.datastax.mgmtapi.client.invoker.ApiClient;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand All @@ -47,14 +51,17 @@ public class HttpManagementConnectionFactory implements IManagementConnectionFac
private final MetricRegistry metricRegistry;
private final HostConnectionCounters hostConnectionCounters;

private ScheduledExecutorService jobStatusPollerExecutor;

private final Set<String> accessibleDatacenters = Sets.newHashSet();

// Constructor for HttpManagementConnectionFactory
public HttpManagementConnectionFactory(AppContext context) {
public HttpManagementConnectionFactory(AppContext context, ScheduledExecutorService jobStatusPollerExecutor) {
this.metricRegistry
= context.metricRegistry == null ? new MetricRegistry() : context.metricRegistry;
hostConnectionCounters = new HostConnectionCounters(metricRegistry);
registerConnectionsGauge();
this.jobStatusPollerExecutor = jobStatusPollerExecutor;
}

public ICassandraManagementProxy connectAny(Collection<Node> nodes) throws ReaperException {
Expand Down Expand Up @@ -118,10 +125,17 @@ private ICassandraManagementProxy connectImpl(Node node)
if (pidResponse.getStatus() != 200) {
throw new ReaperException("Could not get PID for node " + node.getHostname());
}
DefaultApi apiClient = new DefaultApi(
new ApiClient().setBasePath("https://" + node.getHostname() + ":" + managementPort + rootPath));

InstrumentedScheduledExecutorService statusTracker = new InstrumentedScheduledExecutorService(
jobStatusPollerExecutor, metricRegistry);
return new HttpCassandraManagementProxy(
metricRegistry,
rootPath,
new InetSocketAddress(node.getHostname(), managementPort)
new InetSocketAddress(node.getHostname(), managementPort),
statusTracker,
apiClient
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023-2023 DataStax, Inc.
*
* 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 io.cassandrareaper.management.http.models;

import java.util.concurrent.atomic.AtomicInteger;

public class JobStatusTracker {
public AtomicInteger latestNotificationCount = new AtomicInteger(0);
}
Loading
Loading