-
Notifications
You must be signed in to change notification settings - Fork 53
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
[WIP] Add interfaces for refactoring work #1276
base: main
Are you sure you want to change the base?
Changes from all commits
e83b53f
29492e3
14fc451
7d596b7
9ebeb8a
84760dc
44a530f
cfa82ad
ed1667b
7e6fe25
833b384
23c0967
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util; | ||
|
||
/** | ||
* An optional function defining extra cleanup steps to take when a cache item is cleaned up. | ||
* | ||
* @param <V> the type of object being disposed | ||
*/ | ||
public interface ItemDisposalFunc<V> { | ||
void dispose(V item); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util; | ||
|
||
/** | ||
* An optional function defining the conditions under which an expired entry should be cleaned up | ||
* at cleanup time. | ||
* | ||
* @param <V> the type of object being analyzed for disposal | ||
*/ | ||
public interface ShouldDisposeFunc<V> { | ||
boolean shouldDispose(V item); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.events; | ||
|
||
// A marker interface for events that need to be communicated between different components. | ||
public interface Event { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.events; | ||
|
||
import java.util.Set; | ||
|
||
public interface EventPublisher { | ||
/** | ||
* Register the given subscriber for the given event classes. | ||
* | ||
* @param subscriber the subscriber to be notified when the given event classes occur. | ||
* @param eventClasses the classes of event that the subscriber should be notified of. | ||
*/ | ||
void subscribe(EventSubscriber subscriber, Set<Class<? extends Event>> eventClasses); | ||
|
||
/** | ||
* Unsubscribe the subscriber from the given event classes. | ||
* | ||
* @param subscriber the subscriber to unsubscribe from the given event classes. | ||
* @param eventClasses the classes of events that the subscriber wants to unsubscribe from. | ||
*/ | ||
void unsubscribe(EventSubscriber subscriber, Set<Class<? extends Event>> eventClasses); | ||
|
||
/** | ||
* Publish an event. All subscribers to the given event class will be notified of the event. | ||
* | ||
* @param event the event to publish. | ||
*/ | ||
void publish(Event event); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.events; | ||
|
||
public interface EventSubscriber { | ||
/** | ||
* Process an event. This method will only be called on this subscriber if it has subscribed to the event class via | ||
* {@link EventPublisher#subscribe}. | ||
* | ||
* @param event the event to process. | ||
*/ | ||
void processEvent(Event event); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.events; | ||
|
||
public class WriterChangedExampleEvent implements Event { | ||
final String newWriterUrl; | ||
final String oldWriterUrl; | ||
|
||
public WriterChangedExampleEvent(String newWriterUrl, String oldWriterUrl) { | ||
this.newWriterUrl = newWriterUrl; | ||
this.oldWriterUrl = oldWriterUrl; | ||
} | ||
|
||
public String getNewWriterUrl() { | ||
return newWriterUrl; | ||
} | ||
|
||
public String getOldWriterUrl() { | ||
return oldWriterUrl; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.monitoring; | ||
|
||
public interface Monitor { | ||
void start(); | ||
|
||
void stop(); | ||
|
||
MonitorStatus getStatus(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.monitoring; | ||
|
||
public enum MonitorErrorResponse { | ||
NO_ACTION, | ||
RESTART | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.monitoring; | ||
|
||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import software.amazon.jdbc.util.ShouldDisposeFunc; | ||
|
||
public interface MonitorService { | ||
/** | ||
* Register a new monitor type with the monitor service. This method needs to be called before adding new types of | ||
* monitors to the monitor service, so that the monitor service knows when a running monitor should be stopped. | ||
* Expected monitor types ("topology" and "customEndpoint") will be added automatically during driver initialization, | ||
* but this method can be called by users if they want to add a new monitor type. | ||
* | ||
* @param monitorType a String representing the monitor type, eg "customEndpoint". | ||
* @param errorResponses a Set defining actions to take if the monitor is in an error state. | ||
* @param expirationTimeNs how long a monitor should be stored before expiring, in nanoseconds. If the monitor is | ||
* expired and shouldDisposeFunc returns `true`, the monitor will be stopped. | ||
* @param shouldDisposeFunc a function defining whether an item should be stopped if expired. If `null` is passed, the | ||
* monitor will always be stopped if the monitor is expired. | ||
*/ | ||
void registerMonitorTypeIfAbsent( | ||
String monitorType, | ||
Set<MonitorErrorResponse> errorResponses, | ||
long expirationTimeNs, | ||
@Nullable ShouldDisposeFunc<Monitor> shouldDisposeFunc); | ||
|
||
/** | ||
* Creates and starts the given monitor if it does not already exist and stores it under the given monitor type and | ||
* key. | ||
* | ||
* @param monitorType a String representing the monitor type, eg "customEndpoint". | ||
* @param key the key for the monitor, eg | ||
* "custom-endpoint.cluster-custom-XYZ.us-east-2.rds.amazonaws.com:5432". | ||
* @param monitorSupplier an initialization lambda that can be used to create the monitor if it is absent. | ||
*/ | ||
void runIfAbsent( | ||
String monitorType, | ||
Object key, | ||
Supplier<Monitor> monitorSupplier); | ||
|
||
/** | ||
* Stops the given monitor and removes it from the monitor service. | ||
* | ||
* @param monitorType a String representing the monitor type, eg "customEndpoint". | ||
* @param key the key for the monitor, eg | ||
* "custom-endpoint.cluster-custom-XYZ.us-east-2.rds.amazonaws.com:5432". | ||
*/ | ||
void stopAndRemove(String monitorType, Object key); | ||
|
||
/** | ||
* Stops all monitors for the given type and removes them from the monitor service. | ||
* | ||
* @param monitorType a String representing the monitor type, eg "customEndpoint". | ||
*/ | ||
void stopAndRemoveMonitors(String monitorType); | ||
|
||
/** | ||
* Stops all monitors and removes them from the monitor service. | ||
*/ | ||
void stopAndRemoveAll(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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 software.amazon.jdbc.util.monitoring; | ||
|
||
public enum MonitorState { | ||
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. CREATED? 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. Does that indicate a monitor that has been instantiated but never run? With the current design the monitor service doesn't provide a method to submit a monitor without running it, we just have the "runIfAbsent" method which would automatically create and run the monitor. So I'm not sure if we'd ever have a monitor in this state. 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. Theoretically it's possible to create an instance, add it to the list of monitors and let other threads to observe it. In this case the monitor is created but isn't yet started. Otherwise your code has to guarantee that such monitors won't be available for other observers. 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. Currently the API doesn't have a method that creates a monitor but does not run it. The only way to submit a monitor is to call the runIfAbsent method which will automatically start the monitor when it is created. If we discover we need a method that submits a monitor but does not start then yes we should add this state. |
||
RUNNING, | ||
STOPPED, | ||
ERROR | ||
} |
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.
There's an exception as part of MonitorStatus. Is it an unhandled exception that stops/breaks a monitor?
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.
That was my intention yes, it would be a reference to an exception that caused a monitor to unexpectedly stop.
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.
That means it's a handled exception (since it's been caught and stored). That contradicts the original goal. It's possible that monitor isn't able to catch it so monitor service needs to handle them.
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.
Oh, I was thinking the monitor would use a try-catch for its entire run loop that would catch the generic Exception type (eg it would catch any unhandled exceptions) and then it would set its exception status. So the monitor itself would be responsible for indicating it has hit an unexpected error, and the monitor service's watcher thread would check the monitor's status to see if it has hit an error. Do we want to pursue a different approach?