1 /* $Id: lockidx.c,v 1.25 2005-06-14 20:28:54 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
39 static ZebraLockHandle server_lock_main = NULL;
40 static ZebraLockHandle server_lock_cmt = NULL;
41 static ZebraLockHandle server_lock_org = NULL;
43 int zebraIndexWait (ZebraHandle zh, int commitPhase)
48 zebra_unlock (server_lock_cmt);
53 zebra_lock_prefix (zh->service->res, path);
54 strcat (path, FNAME_COMMIT_LOCK);
55 server_lock_cmt = zebra_lock_create (path, 1);
58 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
63 zebra_unlock (server_lock_org);
68 zebra_lock_prefix (zh->service->res, path);
69 strcat (path, FNAME_ORG_LOCK);
70 server_lock_org = zebra_lock_create (path, 1);
73 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
81 if (zebra_lock_nb (h))
84 if (errno != EWOULDBLOCK)
86 yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock");
91 yaz_log (YLOG_LOG, "Waiting for lock cmt");
93 yaz_log (YLOG_LOG, "Waiting for lock org");
94 if (zebra_lock (h) == -1)
96 yaz_log (YLOG_FATAL, "flock");
105 void zebraIndexLockMsg (ZebraHandle zh, const char *str)
110 assert (server_lock_main);
111 fd = zebra_lock_fd (server_lock_main);
112 lseek (fd, 0L, SEEK_SET);
114 r = write (fd, str, l);
117 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write lock file");
120 zebra_lock_prefix (zh->service->res, path);
121 strcat (path, FNAME_TOUCH_TIME);
122 fd = creat (path, 0666);
126 void zebraIndexUnlock (ZebraHandle zh)
130 zebra_lock_prefix (zh->service->res, path);
131 strcat (path, FNAME_MAIN_LOCK);
133 zebra_lock_destroy (server_lock_main);
134 if (unlink (path) && errno != ENOENT)
135 yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
137 if (unlink (path) && errno != ENOENT)
138 yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
139 zebra_lock_destroy (server_lock_main);
141 server_lock_main = 0;
144 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow,
151 if (server_lock_main)
154 zebra_lock_prefix (zh->service->res, path);
155 strcat (path, FNAME_MAIN_LOCK);
158 server_lock_main = zebra_lock_create (path, 2);
159 if (!server_lock_main)
161 server_lock_main = zebra_lock_create (path, 1);
162 if (!server_lock_main)
164 yaz_log (YLOG_FATAL, "couldn't obtain indexer lock");
167 if (zebra_lock_nb (server_lock_main) == -1)
170 yaz_log (YLOG_LOG, "waiting for other index process");
171 zebra_lock (server_lock_main);
172 zebra_unlock (server_lock_main);
173 zebra_lock_destroy (server_lock_main);
176 if (errno == EWOULDBLOCK)
178 yaz_log (YLOG_LOG, "waiting for other index process");
179 zebra_lock (server_lock_main);
180 zebra_unlock (server_lock_main);
181 zebra_lock_destroy (server_lock_main);
186 yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock %s", path);
193 int fd = zebra_lock_fd (server_lock_main);
195 yaz_log (YLOG_WARN, "unlocked %s", path);
196 r = read (fd, buf, 256);
199 yaz_log (YLOG_WARN, "zero length %s", path);
200 zebra_lock_destroy (server_lock_main);
206 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read %s", path);
211 yaz_log (YLOG_WARN, "previous transaction didn't"
213 zebra_lock_destroy (server_lock_main);
214 bf_commitClean (bfs, rval);
218 else if (*buf == 'd')
220 yaz_log (YLOG_WARN, "commit file wan't deleted after commit");
221 zebra_lock_destroy (server_lock_main);
222 bf_commitClean (bfs, rval);
226 else if (*buf == 'w')
229 "The lock file indicates that your index is");
230 yaz_log (YLOG_WARN, "inconsistent. Perhaps the indexer");
231 yaz_log (YLOG_WARN, "terminated abnormally in the previous");
232 yaz_log (YLOG_WARN, "run. You can try to proceed by");
233 yaz_log (YLOG_WARN, "deleting the file %s", path);
236 else if (*buf == 'c')
241 zebra_lock_destroy (server_lock_main);
244 yaz_log (YLOG_FATAL, "previous transaction didn't"
245 " finish commit. Commit now!");
250 yaz_log (YLOG_FATAL, "unknown id 0x%02x in %s", *buf,
259 zebra_lock (server_lock_main);