-
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.
- Loading branch information
1 parent
b85ed13
commit b412a26
Showing
12 changed files
with
350 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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
/* | ||
* 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; | ||
private final static int DEFAULT_ACK_DEADLINE = 600; | ||
Check warning on line 28 in cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/DefaultEventReaderContext.java GitHub Actions / Checkstylecom.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck
Check warning on line 28 in cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/DefaultEventReaderContext.java GitHub Actions / Checkstylecom.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck
Check warning on line 28 in cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/DefaultEventReaderContext.java GitHub Actions / Checkstylecom.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck
|
||
|
||
/** | ||
* 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("ackDeadline", cConf.get(Constants.Event.EVENT_READER_ACK_DEADLINE, | ||
String.valueOf(DEFAULT_ACK_DEADLINE))); | ||
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(); | ||
} |
33 changes: 33 additions & 0 deletions
33
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,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 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. | ||
* | ||
* @return true if successful | ||
*/ | ||
public abstract boolean initialize(); | ||
} |
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
/* | ||
* 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 -> { | ||
// Loading the event readers from provider | ||
// Initialize the event subscribers with all the event readers provided by provider | ||
if (eventSubscriber.initialize()) { | ||
eventSubscriber.startAndWait(); | ||
LOG.info("Successfully initialized eventSubscriber: {}", | ||
eventSubscriber.getClass().getSimpleName()); | ||
} else { | ||
LOG.error("Failed to initialize eventSubscriber: {}", | ||
eventSubscriber.getClass().getSimpleName()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void shutDown() throws Exception { | ||
if (!enabled) { | ||
return; // If not enabled, don't shut down | ||
} | ||
eventSubscribers.forEach(eventSubscriber -> { | ||
eventSubscriber.stopAndWait(); | ||
}); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
cdap-app-fabric/src/main/java/io/cdap/cdap/internal/events/dummy/DummyEventReader.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,87 @@ | ||
/* | ||
* 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.dummy; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.inject.Inject; | ||
import io.cdap.cdap.spi.events.Event; | ||
import io.cdap.cdap.spi.events.EventReader; | ||
import io.cdap.cdap.spi.events.EventReaderContext; | ||
import io.cdap.cdap.spi.events.EventResult; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.function.Consumer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Dummy implementation of {@link EventReader} mainly for test proposals. | ||
*/ | ||
public class DummyEventReader<T extends Event> implements EventReader<T> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DummyEventReader.class); | ||
|
||
@Inject | ||
public DummyEventReader() { | ||
} | ||
|
||
@Override | ||
public void initialize(EventReaderContext eventReaderContext) { | ||
logger.info("Initializing DummyEventReader..."); | ||
} | ||
|
||
@Override | ||
public EventResult<T> pull(int maxMessages) { | ||
ArrayList<T> sentMessages = new ArrayList<>(); | ||
Iterator<T> it = getMessages().iterator(); | ||
int i = 0; | ||
while (i < maxMessages && it.hasNext()) { | ||
sentMessages.add(it.next()); | ||
i++; | ||
} | ||
return new DummyEventResult(sentMessages); | ||
} | ||
|
||
@VisibleForTesting | ||
public Collection<T> getMessages() { | ||
return new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
logger.info("Closing dummy reader"); | ||
} | ||
|
||
class DummyEventResult implements EventResult<T> { | ||
final Collection<T> events; | ||
|
||
DummyEventResult(Collection<T> events) { | ||
this.events = events; | ||
} | ||
|
||
@Override | ||
public void consumeEvents(Consumer<T> consumer) { | ||
for (T evt : events) { | ||
try { | ||
consumer.accept(evt); | ||
} catch (Exception e) { | ||
logger.error("Error on consuming event {}", evt.getVersion(), e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception {} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...c/src/main/java/io/cdap/cdap/internal/events/dummy/DummyEventReaderExtensionProvider.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.dummy; | ||
|
||
import com.google.inject.Inject; | ||
import io.cdap.cdap.internal.events.EventReaderProvider; | ||
import io.cdap.cdap.spi.events.Event; | ||
import io.cdap.cdap.spi.events.EventReader; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Dummy implementation for {@link EventReaderProvider} for test proposals. | ||
*/ | ||
public class DummyEventReaderExtensionProvider<T extends Event> implements EventReaderProvider { | ||
|
||
private final DummyEventReader eventReader; | ||
|
||
@Inject | ||
public DummyEventReaderExtensionProvider(DummyEventReader eventReader) { | ||
this.eventReader = eventReader; | ||
} | ||
|
||
@Override | ||
public Map<String, EventReader> loadEventReaders() { | ||
Map<String, EventReader> map = new HashMap<>(); | ||
map.put(this.eventReader.getClass().getName(), this.eventReader); | ||
return Collections.unmodifiableMap(map); | ||
} | ||
} |
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
Oops, something went wrong.