1 /* $Id: lockidx.c,v 1.24 2005-01-15 19:38:26 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
38 static ZebraLockHandle server_lock_main = NULL;
39 static ZebraLockHandle server_lock_cmt = NULL;
40 static ZebraLockHandle server_lock_org = NULL;
42 int zebraIndexWait (ZebraHandle zh, int commitPhase)
47 zebra_unlock (server_lock_cmt);
52 zebra_lock_prefix (zh->service->res, path);
53 strcat (path, FNAME_COMMIT_LOCK);
54 server_lock_cmt = zebra_lock_create (path, 1);
57 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
62 zebra_unlock (server_lock_org);
67 zebra_lock_prefix (zh->service->res, path);
68 strcat (path, FNAME_ORG_LOCK);
69 server_lock_org = zebra_lock_create (path, 1);
72 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
80 if (zebra_lock_nb (h))
83 if (errno != EWOULDBLOCK)
85 yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock");
90 yaz_log (YLOG_LOG, "Waiting for lock cmt");
92 yaz_log (YLOG_LOG, "Waiting for lock org");
93 if (zebra_lock (h) == -1)
95 yaz_log (YLOG_FATAL, "flock");
104 void zebraIndexLockMsg (ZebraHandle zh, const char *str)
109 assert (server_lock_main);
110 fd = zebra_lock_fd (server_lock_main);
111 lseek (fd, 0L, SEEK_SET);
113 r = write (fd, str, l);
116 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write lock file");
119 zebra_lock_prefix (zh->service->res, path);
120 strcat (path, FNAME_TOUCH_TIME);
121 fd = creat (path, 0666);
125 void zebraIndexUnlock (ZebraHandle zh)
129 zebra_lock_prefix (zh->service->res, path);
130 strcat (path, FNAME_MAIN_LOCK);
132 zebra_lock_destroy (server_lock_main);
133 if (unlink (path) && errno != ENOENT)
134 yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
136 if (unlink (path) && errno != ENOENT)
137 yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
138 zebra_lock_destroy (server_lock_main);
140 server_lock_main = 0;
143 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow,
150 if (server_lock_main)
153 zebra_lock_prefix (zh->service->res, path);
154 strcat (path, FNAME_MAIN_LOCK);
157 server_lock_main = zebra_lock_create (path, 2);
158 if (!server_lock_main)
160 server_lock_main = zebra_lock_create (path, 1);
161 if (!server_lock_main)
163 yaz_log (YLOG_FATAL, "couldn't obtain indexer lock");
166 if (zebra_lock_nb (server_lock_main) == -1)
169 yaz_log (YLOG_LOG, "waiting for other index process");
170 zebra_lock (server_lock_main);
171 zebra_unlock (server_lock_main);
172 zebra_lock_destroy (server_lock_main);
175 if (errno == EWOULDBLOCK)
177 yaz_log (YLOG_LOG, "waiting for other index process");
178 zebra_lock (server_lock_main);
179 zebra_unlock (server_lock_main);
180 zebra_lock_destroy (server_lock_main);
185 yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock %s", path);
192 int fd = zebra_lock_fd (server_lock_main);
194 yaz_log (YLOG_WARN, "unlocked %s", path);
195 r = read (fd, buf, 256);
198 yaz_log (YLOG_WARN, "zero length %s", path);
199 zebra_lock_destroy (server_lock_main);
205 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read %s", path);
210 yaz_log (YLOG_WARN, "previous transaction didn't"
212 zebra_lock_destroy (server_lock_main);
213 bf_commitClean (bfs, rval);
217 else if (*buf == 'd')
219 yaz_log (YLOG_WARN, "commit file wan't deleted after commit");
220 zebra_lock_destroy (server_lock_main);
221 bf_commitClean (bfs, rval);
225 else if (*buf == 'w')
228 "The lock file indicates that your index is");
229 yaz_log (YLOG_WARN, "inconsistent. Perhaps the indexer");
230 yaz_log (YLOG_WARN, "terminated abnormally in the previous");
231 yaz_log (YLOG_WARN, "run. You can try to proceed by");
232 yaz_log (YLOG_WARN, "deleting the file %s", path);
235 else if (*buf == 'c')
240 zebra_lock_destroy (server_lock_main);
243 yaz_log (YLOG_FATAL, "previous transaction didn't"
244 " finish commit. Commit now!");
249 yaz_log (YLOG_FATAL, "unknown id 0x%02x in %s", *buf,
258 zebra_lock (server_lock_main);