-
Notifications
You must be signed in to change notification settings - Fork 217
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
burmanm
merged 4 commits into
integration/http-managementproxy
from
jobdetails_notifications
Aug 28, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d01f3cd
Add stubbed polling of job details from the mgmt-api. This will not w…
burmanm 23fb839
Merge test files after the rebase
burmanm a1446bb
Add a test to verify the behavior of the notifications polling
burmanm 62226dd
Address comments
burmanm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/server/src/main/java/io/cassandrareaper/management/http/models/JobStatusTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
But I'm not a lawyer.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.