monstache
repository·rel6·Indexed 23 days ago
https://github.com/rwynn/monstacheA Go-based daemon designed to synchronize data from MongoDB to Elasticsearch in real-time. Version 6 supports MongoDB 3.6+ and Elasticsearch 7.0+, defaulting to MongoDB change streams for synchronization. It features support for JavaScript-based mapping and filtering, high-performance Go mapper plugins, data relationship denormalization (relates), and GridFs indexing.
What's inside monstache
- monstache is a Go-based daemon designed to synchronize data from MongoDB to Elasticsearch in real-time.
How change streams and namespace watching work in Version 6
rel6Monstache Version 6 defaults to using MongoDB change streams instead of tailing the oplog.
- Default behavior: Without specific configuration, Monstache watches the entire MongoDB deployment.
- Targeted watching: To watch specific namespaces instead of the whole deployment, use the
change-stream-namespacesoption, which accepts an array of strings.
Configure MongoDB connection settings
rel6In Version 6, several MongoDB settings that were previously handled within Monstache have been removed. These settings should now be provided directly via the MongoDB connection string.Prerequisites for the Monstache local builder
rel6To use the
build.shscript, you must have the following installed on your host machine:- Docker
- Bash shell
Build a Monstache Go plugin using Docker
rel6You can build a Go plugin for Monstache using the
build.shscript, which utilizes Docker to ensure a consistent build environment.Workflow:
- Place your plugin source code (a
.gofile with amainpackage) into thedocker/plugindirectory. - Ensure the
.pluginfile contains the name of your plugin file (excluding the.goextension). - Execute
./build.shfrom thedocker/plugindirectory. - The resulting
.sofile will be generated in thedocker-buildfolder.
To activate the plugin in Monstache, provide the path to the generated
.sofile using themapper-plugin-pathargument../build.sh- Place your plugin source code (a
Build Monstache binaries locally using Docker
rel6You can use the
build.shscript to build all Monstache binaries forlinux,win, andmactargets within a Docker container. The script exports the resulting binaries to a local directory nameddocker-build.Note: Running the script will remove any existing
docker-buildfolder before exporting the new one../build.shRequirements for Monstache Version 6
rel6Monstache Version 6 requires the following compatible versions of its data sources:
- MongoDB: 3.6 or higher
- Elasticsearch: 7.0 or higher
It utilizes the official MongoDB Go driver and the community-supported Elasticsearch driver from
olivere.Prerequisites for building Monstache plugins
rel6To use the Monstache plugin builder script, you must have the following installed on your host machine:
- Docker
- Bash shell
Use JavaScript Scripts for Mapping, Filtering, and Pipelines
rel6Monstache supports running JavaScript (via the Otto engine) to transform or filter data. You can provide scripts via the
--scriptconfiguration option (either as an inline string or a file path).Scripts are scoped to a
namespace. Within the script, you must export a function viamodule.exports.- Filters: A function that returns a boolean. If
true, the operation is kept; iffalse, it is dropped. - Scripts (Mapping): A function used to transform the document data before indexing.
- Pipelines: Functions used for complex transformations.
Available helper:
stringFromBinData(binData)can be used to convert MongoDBBinarydata to a string within your script.- Filters: A function that returns a boolean. If
Configure Elasticsearch Deletion Strategies
rel6Monstache supports different strategies for handling document deletions in Elasticsearch, controlled via the
DeleteStrategyconfiguration:ignoreDeleteStrategy: Deletions in MongoDB are ignored and nothing is removed from Elasticsearch.statefulDeleteStrategy: Monstache uses stored metadata (in a MongoDBmetacollection) to find the exact index, routing, and parent ID required to perform a precise deletion. This is the most reliable method for complex routing setups.statelessDeleteStrategy: Monstache attempts to find the document in Elasticsearch using the_idand theDeleteIndexPattern. IfDisableDeleteProtectionis enabled, it usesDeleteByQuery. Otherwise, it performs a search to find the correct index and routing before deleting.
How JSON Patching works in Monstache
rel6When
EnablePatchesis enabled, Monstache can maintain a history of changes using JSON merge patches.For namespaces defined in
PatchNamespaces, Monstache performs the following during an update:- Fetches the existing document from Elasticsearch.
- Calculates a JSON merge patch between the existing document and the new data.
- Appends the patch to a list stored in the document under the key defined by
MergePatchAttr(defaults tojson-merge-patches). - Each patch entry includes a timestamp (
ts) and a version (v).
This allows for reconstructing document states or auditing changes over time.
How Monstache manages event loops and concurrency
rel6Monstache operates using a central
eventLoopthat orchestrates several concurrent processes via Go channels. The main loop listens for various signals including:gtmCtx.OpC: Incoming MongoDB operations (Oplog or Change Streams).gtmCtx.ErrC: Errors from the GTM (Go Tailer Manager) context.timestampTicker: Triggers periodic saving of resume tokens or timestamps based on theResumeStrategy.heartBeat: Manages cluster-mode enabled/disabled states.printStats: Triggers periodic statistics logging.bulkBackoffC: Handles backoff logic for Elasticsearch bulk operations.
Concurrency is achieved through specialized worker pools:
- Indexing Workers: Managed via
startIndex(), which spawns multiple goroutines to consume fromindexCand calldoIndex. - Relate Workers: If
config.Relateis configured,startRelate()spawns workers to consume fromrelateCand callprocessRelated. - Shard Listeners: If
readShardsis enabled, Monstache adds listeners to handle shard-specific events.