-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(es): Support ElasticsearchEventStore (#983)
- Loading branch information
Showing
17 changed files
with
409 additions
and
51 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
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
146 changes: 146 additions & 0 deletions
146
...search/src/main/kotlin/me/ahoo/wow/elasticsearch/eventsourcing/ElasticsearchEventStore.kt
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,146 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.wow.elasticsearch.eventsourcing | ||
|
||
import co.elastic.clients.elasticsearch._types.OpType | ||
import co.elastic.clients.elasticsearch._types.Refresh | ||
import co.elastic.clients.elasticsearch._types.SortOrder | ||
import me.ahoo.wow.api.modeling.AggregateId | ||
import me.ahoo.wow.api.modeling.NamedAggregate | ||
import me.ahoo.wow.elasticsearch.IndexNameConverter.toEventStreamIndexName | ||
import me.ahoo.wow.elasticsearch.query.ElasticsearchConditionConverter.toQuery | ||
import me.ahoo.wow.elasticsearch.query.ElasticsearchSortConverter.toSortOptions | ||
import me.ahoo.wow.event.DomainEventStream | ||
import me.ahoo.wow.eventsourcing.AbstractEventStore | ||
import me.ahoo.wow.eventsourcing.AggregateIdScanner.Companion.FIRST_CURSOR_ID | ||
import me.ahoo.wow.eventsourcing.EventVersionConflictException | ||
import me.ahoo.wow.query.dsl.condition | ||
import me.ahoo.wow.query.dsl.sort | ||
import me.ahoo.wow.serialization.MessageRecords | ||
import org.elasticsearch.client.ResponseException | ||
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import reactor.kotlin.core.publisher.toMono | ||
|
||
class ElasticsearchEventStore( | ||
private val elasticsearchClient: ReactiveElasticsearchClient, | ||
private val refreshPolicy: Refresh = Refresh.True | ||
) : AbstractEventStore() { | ||
companion object { | ||
private const val VERSION_CONFLICT_CODE = 409 | ||
private const val DEFAULT_BATCH_SIZE = 100 | ||
} | ||
|
||
private fun DomainEventStream.toDocId(): String { | ||
return "${this.aggregateId.id}-${this.version}" | ||
} | ||
|
||
override fun appendStream(eventStream: DomainEventStream): Mono<Void> { | ||
return elasticsearchClient.index { | ||
it.index(eventStream.aggregateId.toEventStreamIndexName()) | ||
.id(eventStream.toDocId()) | ||
.document(eventStream) | ||
.opType(OpType.Create) | ||
.refresh(refreshPolicy) | ||
}.onErrorResume { | ||
if (it is ResponseException && it.response.statusLine.statusCode == VERSION_CONFLICT_CODE) { | ||
return@onErrorResume EventVersionConflictException( | ||
eventStream = eventStream, | ||
cause = it, | ||
).toMono() | ||
} | ||
Mono.error(it) | ||
}.then() | ||
} | ||
|
||
override fun loadStream( | ||
aggregateId: AggregateId, | ||
headVersion: Int, | ||
tailVersion: Int | ||
): Flux<DomainEventStream> { | ||
return loopStream(aggregateId, headVersion, tailVersion) | ||
} | ||
|
||
private fun loopStream( | ||
aggregateId: AggregateId, | ||
headVersion: Int, | ||
tailVersion: Int | ||
): Flux<DomainEventStream> { | ||
var endVersion = headVersion + DEFAULT_BATCH_SIZE - 1 | ||
if (tailVersion < endVersion) { | ||
endVersion = tailVersion | ||
} | ||
return findEventStream(aggregateId, headVersion, endVersion).flatMapMany { | ||
val previousStreams = Flux.fromIterable(it) | ||
val requestSize = endVersion - headVersion + 1 | ||
if (it.size < requestSize) { | ||
return@flatMapMany previousStreams | ||
} | ||
val lastVersion = it.last().version | ||
if (lastVersion >= tailVersion) { | ||
return@flatMapMany previousStreams | ||
} | ||
val nextStreams = loopStream(aggregateId, lastVersion + 1, tailVersion) | ||
return@flatMapMany previousStreams.concatWith(nextStreams) | ||
} | ||
} | ||
|
||
private fun findEventStream( | ||
aggregateId: AggregateId, | ||
headVersion: Int, | ||
tailVersion: Int | ||
): Mono<List<DomainEventStream>> { | ||
val query = condition { | ||
tenantId(aggregateId.tenantId) | ||
MessageRecords.AGGREGATE_ID eq aggregateId.id | ||
MessageRecords.VERSION between headVersion to tailVersion | ||
}.toQuery() | ||
val sort = sort { MessageRecords.VERSION.asc() }.toSortOptions() | ||
return elasticsearchClient.search<DomainEventStream>({ | ||
it.index(aggregateId.toEventStreamIndexName()) | ||
.query(query) | ||
.size(DEFAULT_BATCH_SIZE) | ||
.sort(sort) | ||
}, DomainEventStream::class.java).map<List<DomainEventStream>> { | ||
it.hits().hits().map { hit -> hit.source() as DomainEventStream } | ||
} | ||
} | ||
|
||
override fun scanAggregateId( | ||
namedAggregate: NamedAggregate, | ||
cursorId: String, | ||
limit: Int | ||
): Flux<AggregateId> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun tailCursorId(namedAggregate: NamedAggregate): Mono<String> { | ||
return elasticsearchClient.search({ searchBuilder -> | ||
searchBuilder.index(namedAggregate.toEventStreamIndexName()) | ||
.size(1) | ||
.sort { | ||
it.field { | ||
it.field(MessageRecords.AGGREGATE_ID).order(SortOrder.Desc) | ||
} | ||
} | ||
.source { | ||
it.fetch(false) | ||
} | ||
}, Map::class.java).map<String> { | ||
val hit = it.hits().hits().firstOrNull() ?: return@map FIRST_CURSOR_ID | ||
hit.sort().first().stringValue() | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
wow-elasticsearch/src/main/resources/templates/wow-aggregate-id-template.json
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,28 @@ | ||
{ | ||
"index_patterns": [ | ||
"wow.*.id" | ||
], | ||
"template": { | ||
"settings": { | ||
"number_of_shards": 3, | ||
"number_of_replicas": 2 | ||
}, | ||
"mappings": { | ||
"properties": { | ||
"tenantId": { | ||
"type": "keyword" | ||
} | ||
}, | ||
"dynamic_templates": [ | ||
{ | ||
"string_as_keyword": { | ||
"match_mapping_type": "string", | ||
"mapping": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.