-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from agorapulse/fix/on-leader-only-not-working
Improved reliability of leader selection
- Loading branch information
Showing
12 changed files
with
426 additions
and
115 deletions.
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
73 changes: 73 additions & 0 deletions
73
...ker-tck/src/main/groovy/com/agorapulse/worker/tck/executor/JobExecutorEventCollector.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,73 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2021-2024 Agorapulse. | ||
* | ||
* 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 | ||
* | ||
* https://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 com.agorapulse.worker.tck.executor; | ||
|
||
import com.agorapulse.worker.event.JobExecutorEvent; | ||
import io.micronaut.context.annotation.Primary; | ||
import io.micronaut.context.event.ApplicationEventPublisher; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.runtime.context.scope.Refreshable; | ||
import io.micronaut.runtime.context.scope.refresh.RefreshEvent; | ||
import io.micronaut.runtime.context.scope.refresh.RefreshEventListener; | ||
import io.micronaut.runtime.event.annotation.EventListener; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Primary | ||
@Singleton | ||
@Refreshable | ||
public class JobExecutorEventCollector implements ApplicationEventPublisher<JobExecutorEvent>, RefreshEventListener { | ||
|
||
private final List<JobExecutorEvent> events = new ArrayList<>(); | ||
|
||
/** | ||
* For usage from micronaut tests. | ||
* @param event the event to be collected | ||
*/ | ||
@EventListener | ||
void onEvent(JobExecutorEvent event) { | ||
events.add(event); | ||
} | ||
|
||
/** | ||
* For direct usage in unit tests | ||
* @param event The event to publish | ||
*/ | ||
@Override | ||
public void publishEvent(@NonNull JobExecutorEvent event) { | ||
events.add(event); | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(RefreshEvent event) { | ||
events.clear(); | ||
} | ||
|
||
public List<JobExecutorEvent> getEvents() { | ||
return events; | ||
} | ||
|
||
@Override | ||
public @NonNull Set<String> getObservedConfigurationPrefixes() { | ||
return Set.of(); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
libs/micronaut-worker/src/main/java/com/agorapulse/worker/event/JobExecutorEvent.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,84 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2021-2024 Agorapulse. | ||
* | ||
* 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 | ||
* | ||
* https://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 com.agorapulse.worker.event; | ||
|
||
import com.agorapulse.worker.JobRunStatus; | ||
|
||
public class JobExecutorEvent { | ||
|
||
public enum Execution { | ||
EXECUTE, SKIP | ||
} | ||
|
||
public enum Type { | ||
LEADER_ONLY, FOLLOWER_ONLY, CONCURRENT | ||
} | ||
|
||
public static JobExecutorEvent leaderOnly(String executor, Execution outcome, JobRunStatus status, String executorId) { | ||
return new JobExecutorEvent(executor, Type.LEADER_ONLY, outcome, status, 0, executorId); | ||
} | ||
|
||
public static JobExecutorEvent followerOnly(String executor, Execution outcome, JobRunStatus status, String executorId) { | ||
return new JobExecutorEvent(executor, Type.FOLLOWER_ONLY, outcome, status, 0, executorId); | ||
} | ||
|
||
public static JobExecutorEvent concurrent(String executor, Execution outcome, JobRunStatus status, int concurrency, String executorId) { | ||
return new JobExecutorEvent(executor, Type.CONCURRENT, outcome, status, concurrency, executorId); | ||
} | ||
|
||
private final String executor; | ||
private final JobRunStatus status; | ||
private final int concurrency; | ||
private final Execution execution; | ||
private final Type type; | ||
private final String executorId; | ||
|
||
public JobExecutorEvent(String executor, Type type, Execution execution, JobRunStatus status, int concurrency, String executorId) { | ||
this.executor = executor; | ||
this.status = status; | ||
this.concurrency = concurrency; | ||
this.execution = execution; | ||
this.type = type; | ||
this.executorId = executorId; | ||
} | ||
|
||
public String getExecutor() { | ||
return executor; | ||
} | ||
|
||
public JobRunStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public int getConcurrency() { | ||
return concurrency; | ||
} | ||
|
||
public Execution getExecution() { | ||
return execution; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
|
||
public String getExecutorId() { | ||
return executorId; | ||
} | ||
|
||
} |
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
Oops, something went wrong.