[syslog-ng] [PATCH (3.4) 4/5] afmongodb: Add support for safe-mode.

Gergely Nagy algernon at balabit.hu
Tue Jan 3 01:55:32 CET 2012


This adds a new option to the mongodb destination driver:
safe-mode(yes|no), which, as the name implies, can be used to turn
safe-mode on and off. For performance reasons, it is off by default.

Safe mode basically involves an extra check after each insert, that
checks whether the last insert succeeded, and only treating the insert
a complete success when the double check succeeds too.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 modules/afmongodb/afmongodb-grammar.ym |    2 ++
 modules/afmongodb/afmongodb-parser.c   |    1 +
 modules/afmongodb/afmongodb.c          |   14 +++++++++++++-
 modules/afmongodb/afmongodb.h          |    1 +
 4 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/modules/afmongodb/afmongodb-grammar.ym b/modules/afmongodb/afmongodb-grammar.ym
index 1a929ac..a21e23c 100644
--- a/modules/afmongodb/afmongodb-grammar.ym
+++ b/modules/afmongodb/afmongodb-grammar.ym
@@ -51,6 +51,7 @@ extern ValuePairsTransformSet *last_vp_transset;
 %token KW_MONGODB
 %token KW_COLLECTION
 %token KW_SERVERS
+%token KW_SAFE_MODE
 
 %%
 
@@ -73,6 +74,7 @@ afmongodb_option
 	| KW_COLLECTION '(' string ')'		{ afmongodb_dd_set_collection(last_driver, $3); free($3); }
 	| KW_USERNAME '(' string ')'		{ afmongodb_dd_set_user(last_driver, $3); free($3); }
 	| KW_PASSWORD '(' string ')'		{ afmongodb_dd_set_password(last_driver, $3); free($3); }
+	| KW_SAFE_MODE '(' yesno ')'		{ afmongodb_dd_set_safe_mode(last_driver, $3); }
 	| value_pair_option			{ afmongodb_dd_set_value_pairs(last_driver, $1); }
 	| dest_driver_option
         ;
diff --git a/modules/afmongodb/afmongodb-parser.c b/modules/afmongodb/afmongodb-parser.c
index b589fe8..b8c9ead 100644
--- a/modules/afmongodb/afmongodb-parser.c
+++ b/modules/afmongodb/afmongodb-parser.c
@@ -35,6 +35,7 @@ static CfgLexerKeyword afmongodb_keywords[] = {
   { "collection",		KW_COLLECTION },
   { "username",			KW_USERNAME },
   { "password",			KW_PASSWORD },
+  { "safe_mode",		KW_SAFE_MODE },
   { NULL }
 };
 
diff --git a/modules/afmongodb/afmongodb.c b/modules/afmongodb/afmongodb.c
index 01d5dc8..1a7dd0e 100644
--- a/modules/afmongodb/afmongodb.c
+++ b/modules/afmongodb/afmongodb.c
@@ -53,6 +53,8 @@ typedef struct
   gchar *host;
   gint port;
 
+  gboolean safe_mode;
+
   gchar *user;
   gchar *password;
 
@@ -146,6 +148,14 @@ afmongodb_dd_set_value_pairs(LogDriver *d, ValuePairs *vp)
   self->vp = vp;
 }
 
+void
+afmongodb_dd_set_safe_mode(LogDriver *d, gboolean state)
+{
+  MongoDBDestDriver *self = (MongoDBDestDriver *)d;
+
+  self->safe_mode = state;
+}
+
 /*
  * Utilities
  */
@@ -195,13 +205,14 @@ afmongodb_dd_connect(MongoDBDestDriver *self, gboolean reconnect)
     return TRUE;
 
   self->conn = mongo_sync_connect(self->host, self->port, FALSE);
-
   if (!self->conn)
     {
       msg_error ("Error connecting to MongoDB", NULL);
       return FALSE;
     }
 
+  mongo_sync_conn_set_safe_mode(self->conn, self->safe_mode);
+
   l = self->servers;
   while ((l = g_list_next(l)) != NULL)
     {
@@ -572,6 +583,7 @@ afmongodb_dd_new(void)
   afmongodb_dd_set_servers((LogDriver *)self, g_list_append (NULL, g_strdup ("127.0.0.1:27017")));
   afmongodb_dd_set_database((LogDriver *)self, "syslog");
   afmongodb_dd_set_collection((LogDriver *)self, "messages");
+  afmongodb_dd_set_safe_mode((LogDriver *)self, FALSE);
 
   init_sequence_number(&self->seq_num);
 
diff --git a/modules/afmongodb/afmongodb.h b/modules/afmongodb/afmongodb.h
index 4aa47ef..9950d26 100644
--- a/modules/afmongodb/afmongodb.h
+++ b/modules/afmongodb/afmongodb.h
@@ -35,5 +35,6 @@ void afmongodb_dd_set_collection(LogDriver *d, const gchar *collection);
 void afmongodb_dd_set_user(LogDriver *d, const gchar *user);
 void afmongodb_dd_set_password(LogDriver *d, const gchar *password);
 void afmongodb_dd_set_value_pairs(LogDriver *d, ValuePairs *vp);
+void afmongodb_dd_set_safe_mode(LogDriver *d, gboolean state);
 
 #endif
-- 
1.7.7.3



More information about the syslog-ng mailing list