tracker r1375 - in branches/indexer-split: . src/libtracker-common src/trackerd



Author: ifrade
Date: Thu May  8 14:07:21 2008
New Revision: 1375
URL: http://svn.gnome.org/viewvc/tracker?rev=1375&view=rev

Log:
Moved NFS lock functions to its own module in libtracker-common

Added:
   branches/indexer-split/src/libtracker-common/tracker-nfs-lock.c
   branches/indexer-split/src/libtracker-common/tracker-nfs-lock.h
Modified:
   branches/indexer-split/ChangeLog
   branches/indexer-split/src/libtracker-common/Makefile.am
   branches/indexer-split/src/trackerd/tracker-db-sqlite.c
   branches/indexer-split/src/trackerd/tracker-main.c

Modified: branches/indexer-split/src/libtracker-common/Makefile.am
==============================================================================
--- branches/indexer-split/src/libtracker-common/Makefile.am	(original)
+++ branches/indexer-split/src/libtracker-common/Makefile.am	Thu May  8 14:07:21 2008
@@ -23,6 +23,8 @@
 	tracker-language.h		\
 	tracker-log.c	 		\
 	tracker-log.h			\
+	tracker-nfs-lock.c		\
+	tracker-nfs-lock.h		\
 	tracker-os-dependant.h		\
 	tracker-type-utils.c		\
 	tracker-type-utils.h		\

Added: branches/indexer-split/src/libtracker-common/tracker-nfs-lock.c
==============================================================================
--- (empty file)
+++ branches/indexer-split/src/libtracker-common/tracker-nfs-lock.c	Thu May  8 14:07:21 2008
@@ -0,0 +1,185 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc gnome org)
+ * Copyright (C) 2008, Nokia
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+#include "tracker-nfs-lock.h"
+#include "tracker-log.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <time.h>
+#include <glib/gstdio.h>
+
+static gchar *lock_file = NULL;
+static gchar *tmp_filepath = NULL;
+
+gboolean use_nfs_safe_locking = FALSE;
+
+
+/* get no of links to a file - used for safe NFS atomic file locking */
+static gint
+get_nlinks (const gchar *name)
+{
+	struct stat st;
+
+	if (g_stat (name, &st) == 0) {
+		return st.st_nlink;
+	} else {
+		return -1;
+	}
+}
+
+
+static gint
+get_mtime (const gchar *name)
+{
+	struct stat st;
+
+	if (g_stat (name, &st) == 0) {
+		return st.st_mtime;
+	} else {
+		return -1;
+	}
+}
+
+static gboolean
+is_initialized () 
+{
+        return (lock_file != NULL && tmp_filepath != NULL);
+}
+
+/* serialises db access via a lock file for safe use on (lock broken) NFS mounts */
+gboolean
+tracker_nfs_lock_obtain (void)
+{
+	gint attempt;
+	gchar *tmp_file;
+        gint fd;
+
+	if (!use_nfs_safe_locking) {
+		return TRUE;
+	}
+
+        if (!is_initialized()) {
+                tracker_error ("Trying to use NFS Lock. It is NOT initialized\n");
+                return FALSE;
+        }
+ 
+	tmp_file = g_strdup_printf ("%s_%d.lock", 
+                                    tmp_filepath, 
+                                    (guint32) getpid ());
+
+	for (attempt = 0; attempt < 10000; ++attempt) {
+
+		/* delete existing lock file if older than 5 mins */
+		if (g_file_test (lock_file, G_FILE_TEST_EXISTS) 
+                    && ( time((time_t *) NULL) - get_mtime (lock_file)) > 300) {
+			g_unlink (lock_file);
+		}
+
+		fd = g_open (lock_file, O_CREAT|O_EXCL, 0644);
+
+		if (fd >= 0) {
+
+			/* create host specific file and link to lock file */
+                        if (link (lock_file, tmp_file) == -1) {
+                                goto error;
+                        }
+
+			/* for atomic NFS-safe locks, stat links = 2 if file locked. If greater than 2 then we have a race condition */
+			if (get_nlinks (lock_file) == 2) {
+				close (fd);
+				g_free (tmp_file);
+
+				return TRUE;
+			} else {
+				close (fd);
+				g_usleep (g_random_int_range (1000, 100000));
+			}
+		}
+	}
+
+ error:
+	tracker_error ("ERROR: lock failure");
+	g_free (tmp_file);
+
+	return FALSE;
+}
+
+
+void
+tracker_nfs_lock_release (void)
+{
+	char *tmp_file;
+
+	if (!use_nfs_safe_locking) {
+		return;
+	}
+ 
+        if (!is_initialized()) {
+                tracker_error ("Trying to use NFS Lock. It is NOT initialized\n");
+                return;
+        }
+ 
+	tmp_file = g_strdup_printf ("%s_%d.lock", tmp_filepath, (guint32) getpid ());
+
+	unlink (tmp_file);
+	unlink (lock_file);
+
+	g_free (tmp_file);
+}
+
+void 
+tracker_nfs_lock_init (const gchar *root_dir)
+{
+        if (is_initialized ()) {
+                tracker_error ("Trying to initialize tracker_db_lock for second time");
+        }
+
+        if (lock_file == NULL) {
+                lock_file = g_build_filename (root_dir, "tracker.lock", NULL);
+        }
+        if (tmp_filepath == NULL) {
+                tmp_filepath = g_build_filename (root_dir, g_get_host_name (), NULL);
+        }
+
+        tracker_log ("Initialized NFS lock %s", 
+                     (use_nfs_safe_locking ? "" : "(Not in use)"));
+}
+
+void
+tracker_nfs_lock_term (void)
+{
+        if (!is_initialized ()){
+                tracker_error ("Trying to term tracker_db_lock not initialized");
+        }
+
+        if (lock_file) {
+                g_free (lock_file);
+        }
+
+        if (tmp_filepath) {
+                g_free (tmp_filepath);
+        }
+
+        tracker_log ("Tracker db NFS lock finalized");
+
+}

Added: branches/indexer-split/src/libtracker-common/tracker-nfs-lock.h
==============================================================================
--- (empty file)
+++ branches/indexer-split/src/libtracker-common/tracker-nfs-lock.h	Thu May  8 14:07:21 2008
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc gnome org)
+ * Copyright (C) 2008, Nokia
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+#ifndef __LIBTRACKER_COMMON_NFS_LOCK_H__
+#define __LIBTRACKER_COMMON_NFS_LOCK_H__
+
+#include <glib.h>
+
+void     tracker_nfs_lock_init    (const gchar *root_dir);
+gboolean tracker_nfs_lock_obtain  (void);
+void     tracker_nfs_lock_release (void);
+void     tracker_nfs_lock_term    (void);
+
+#endif

Modified: branches/indexer-split/src/trackerd/tracker-db-sqlite.c
==============================================================================
--- branches/indexer-split/src/trackerd/tracker-db-sqlite.c	(original)
+++ branches/indexer-split/src/trackerd/tracker-db-sqlite.c	Thu May  8 14:07:21 2008
@@ -24,16 +24,7 @@
 
 #include "config.h"
 
-#include <stdarg.h>
-#include <stdio.h>
-#include <string.h>
-#include <strings.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
-#include <time.h>
 #include <regex.h>
 #include <zlib.h>
 
@@ -49,6 +40,7 @@
 #include <libtracker-common/tracker-file-utils.h>
 #include <libtracker-common/tracker-type-utils.h>
 #include <libtracker-common/tracker-utils.h>
+#include <libtracker-common/tracker-nfs-lock.h>
 
 #include <libtracker-db/tracker-db-interface-sqlite.h>
 
@@ -76,8 +68,6 @@
 static GHashTable *prepared_queries;
 //static GMutex *sequence_mutex;
 
-gboolean use_nfs_safe_locking = FALSE;
-
 typedef struct {
 	guint32		service_id;
 	int		service_type_id;
@@ -1150,118 +1140,13 @@
 }
 
 
-/* get no of links to a file - used for safe NFS atomic file locking */
-static int
-get_nlinks (const char *name)
-{
-	struct stat st;
-
-	if (g_stat (name, &st) == 0) {
-		return st.st_nlink;
-	} else {
-		return -1;
-	}
-}
-
-
-static int
-get_mtime (const char *name)
-{
-	struct stat st;
-
-	if (g_stat (name, &st) == 0) {
-		return st.st_mtime;
-	} else {
-		return -1;
-	}
-}
-
-
-/* serialises db access via a lock file for safe use on (lock broken) NFS mounts */
-static gboolean
-lock_db (void)
-{
-	int attempt;
-	char *lock_file, *tmp, *tmp_file;
-
-	if (!use_nfs_safe_locking) {
-		return TRUE;
-	}
-
-	lock_file = g_build_filename (tracker->root_dir, "tracker.lock", NULL);
-	tmp = g_build_filename (tracker->root_dir, g_get_host_name (), NULL);
-	tmp_file = g_strdup_printf ("%s_%d.lock", tmp, (guint32) getpid ());
-	g_free (tmp);
-
-	for (attempt = 0; attempt < 10000; ++attempt) {
-		int fd;
-
-		/* delete existing lock file if older than 5 mins */
-		if (g_file_test (lock_file, G_FILE_TEST_EXISTS) && ( time((time_t *) NULL) - get_mtime (lock_file)) > 300) {
-			g_unlink (lock_file);
-		}
-
-		fd = g_open (lock_file, O_CREAT|O_EXCL, 0644);
-
-		if (fd >= 0) {
-
-			/* create host specific file and link to lock file */
-                        if (link (lock_file, tmp_file) == -1) {
-                                goto error;
-                        }
-
-			/* for atomic NFS-safe locks, stat links = 2 if file locked. If greater than 2 then we have a race condition */
-			if (get_nlinks (lock_file) == 2) {
-				close (fd);
-				g_free (lock_file);
-				g_free (tmp_file);
-
-				return TRUE;
-			} else {
-				close (fd);
-				g_usleep (g_random_int_range (1000, 100000));
-			}
-		}
-	}
-
- error:
-	tracker_error ("ERROR: lock failure");
-	g_free (lock_file);
-	g_free (tmp_file);
-
-	return FALSE;
-}
-
-
-static void
-unlock_db (void)
-{
-	char *lock_file, *tmp, *tmp_file;
-
-	if (!use_nfs_safe_locking) {
-		return;
-	}
-
-	lock_file = g_build_filename (tracker->root_dir, "tracker.lock", NULL);
-	tmp = g_build_filename (tracker->root_dir,  g_get_host_name (), NULL);
-	tmp_file = g_strdup_printf ("%s_%d.lock", tmp, (guint32) getpid ());
-	g_free (tmp);
-
-	unlink (tmp_file);
-	unlink (lock_file);
-
-	g_free (tmp_file);
-	g_free (lock_file);
-}
-
-
 gboolean
 tracker_db_exec_no_reply (DBConnection *db_con, const char *query, ...)
 {
 	TrackerDBResultSet *result_set;
 	va_list args;
 
-	lock_db();
+	tracker_nfs_lock_obtain ();
 
 	va_start (args, query);
 	result_set = tracker_db_interface_execute_vquery (db_con->db, NULL, query, args);
@@ -1274,7 +1159,7 @@
 		g_object_unref (result_set);
 	}
 
-	unlock_db ();
+	tracker_nfs_lock_release ();
 
 	return TRUE;
 }

Modified: branches/indexer-split/src/trackerd/tracker-main.c
==============================================================================
--- branches/indexer-split/src/trackerd/tracker-main.c	(original)
+++ branches/indexer-split/src/trackerd/tracker-main.c	Thu May  8 14:07:21 2008
@@ -44,6 +44,7 @@
 #include <libtracker-common/tracker-language.h>
 #include <libtracker-common/tracker-log.h>
 #include <libtracker-common/tracker-file-utils.h>
+#include <libtracker-common/tracker-nfs-lock.h>
 
 #include "tracker-dbus.h"
 #include "tracker-email.h"
@@ -298,6 +299,8 @@
 
 	tracker_log_term ();
 
+	tracker_nfs_lock_term ();
+
 	/* remove sys tmp directory */
 	if (tracker->sys_tmp_root_dir) {
 		tracker_dir_remove (tracker->sys_tmp_root_dir);
@@ -646,6 +649,8 @@
                           fatal_errors);
 	tracker_log ("Starting log");
 
+	tracker_nfs_lock_init (tracker->root_dir);
+
         /* Set up mutexes and intitial state */
  	tracker->is_running = FALSE;
 	tracker->shutdown = FALSE;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]