-
Notifications
You must be signed in to change notification settings - Fork 340
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 #15223 from cdapio/feature/cdap-20678-event-pr
Event Subscriber Infrastructure
- Loading branch information
Showing
9 changed files
with
216 additions
and
3 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
45 changes: 45 additions & 0 deletions
45
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/DefaultEventReaderContext.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,45 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, 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.cdap.cdap.internal.events; | ||
|
||
import io.cdap.cdap.common.conf.CConfiguration; | ||
import io.cdap.cdap.common.conf.Constants; | ||
import io.cdap.cdap.spi.events.EventReaderContext; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Provides an initialized default context for EventReader implementing {@link EventReaderContext}. | ||
*/ | ||
public class DefaultEventReaderContext implements EventReaderContext { | ||
|
||
private final Map<String, String> properties; | ||
|
||
/** | ||
* Construct the default Event reader context. | ||
* | ||
* @param prefix prefix for specific event reader | ||
* @param cConf An instance of an injected ${@link CConfiguration}. | ||
*/ | ||
DefaultEventReaderContext(String prefix, CConfiguration cConf) { | ||
Map<String, String> mutableProperties = new HashMap<>(cConf.getPropsWithPrefix(prefix)); | ||
mutableProperties.put(Constants.INSTANCE_NAME, cConf.get(Constants.INSTANCE_NAME)); | ||
this.properties = Collections.unmodifiableMap(mutableProperties); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getProperties() { | ||
return properties; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/EventReaderProvider.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,33 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, 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.cdap.cdap.internal.events; | ||
|
||
import io.cdap.cdap.spi.events.Event; | ||
import io.cdap.cdap.spi.events.EventReader; | ||
import java.util.Map; | ||
|
||
/** | ||
* This interface provides Event Readers ({@link EventReader}) in order to load them as an | ||
* extension. | ||
*/ | ||
public interface EventReaderProvider<T extends Event> { | ||
|
||
/** | ||
* Method which retrieve a {@link Map} of {@link EventReader} and the Extension Type. | ||
*/ | ||
Map<String, EventReader<T>> loadEventReaders(); | ||
} |
31 changes: 31 additions & 0 deletions
31
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/EventSubscriber.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,31 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, 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.cdap.cdap.internal.events; | ||
|
||
import com.google.common.util.concurrent.AbstractScheduledService; | ||
|
||
/** | ||
* Abstract class for an event subscriber. | ||
* Each EventSubscriber will receive and process incoming events. | ||
*/ | ||
public abstract class EventSubscriber extends AbstractScheduledService { | ||
|
||
/** | ||
* Initialize this handler. | ||
*/ | ||
public abstract void initialize(); | ||
} |
76 changes: 76 additions & 0 deletions
76
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/EventSubscriberManager.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,76 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, 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.cdap.cdap.internal.events; | ||
|
||
import com.google.common.util.concurrent.AbstractIdleService; | ||
import com.google.inject.Inject; | ||
import io.cdap.cdap.common.conf.CConfiguration; | ||
import io.cdap.cdap.common.feature.DefaultFeatureFlagsProvider; | ||
import io.cdap.cdap.features.Feature; | ||
import java.util.Set; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* EventSubscriberManager is responsible for starting all the event subscriber threads. | ||
*/ | ||
public class EventSubscriberManager extends AbstractIdleService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(EventSubscriberManager.class); | ||
|
||
private final boolean enabled; | ||
private final Set<EventSubscriber> eventSubscribers; | ||
|
||
@Inject | ||
EventSubscriberManager(CConfiguration cConf, Set<EventSubscriber> eventSubscribers) { | ||
this.enabled = Feature.EVENT_READER.isEnabled(new DefaultFeatureFlagsProvider(cConf)); | ||
this.eventSubscribers = eventSubscribers; | ||
} | ||
|
||
@Override | ||
protected void startUp() throws Exception { | ||
if (!enabled) { | ||
return; // If not enabled, don't start | ||
} | ||
eventSubscribers.forEach(eventSubscriber -> { | ||
// Initialize the event subscribers with all the event readers provided by provider | ||
try { | ||
eventSubscriber.initialize(); | ||
eventSubscriber.startAndWait(); | ||
LOG.info("Successfully initialized eventSubscriber: {}", | ||
eventSubscriber.getClass().getSimpleName()); | ||
} catch (Exception e) { | ||
LOG.error("Failed to initialize eventSubscriber: {}", | ||
eventSubscriber.getClass().getSimpleName(), e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void shutDown() throws Exception { | ||
if (!enabled) { | ||
return; // If not enabled, don't shut down | ||
} | ||
eventSubscribers.forEach(eventSubscriber -> { | ||
try { | ||
eventSubscriber.stopAndWait(); | ||
} catch (Exception e) { | ||
LOG.error("Failed to stop subscriber", e); | ||
} | ||
}); | ||
} | ||
} |
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