219ae6049de23876639bed2b71619a8235a3c0e0
[idzebra-moved-to-github.git] / index / lockidx.c
1 /* $Id: lockidx.c,v 1.23 2004-11-19 10:26:58 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #ifdef WIN32
27 #include <io.h>
28 #else
29 #include <unistd.h>
30 #endif
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "index.h"
36 #include "zserver.h"
37
38 static ZebraLockHandle server_lock_main = NULL;
39 static ZebraLockHandle server_lock_cmt = NULL;
40 static ZebraLockHandle server_lock_org = NULL;
41
42 int zebraIndexWait (ZebraHandle zh, int commitPhase)
43 {
44     ZebraLockHandle h;
45
46     if (server_lock_cmt)
47         zebra_unlock (server_lock_cmt);
48     else
49     {
50         char path[1024];
51
52         zebra_lock_prefix (zh->service->res, path);
53         strcat (path, FNAME_COMMIT_LOCK);
54         server_lock_cmt = zebra_lock_create (path, 1);
55         if (!server_lock_cmt)
56         {
57             yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
58             return -1;
59         }
60     }
61     if (server_lock_org)
62         zebra_unlock (server_lock_org);
63     else
64     {
65         char path[1024];
66
67         zebra_lock_prefix (zh->service->res, path);
68         strcat (path, FNAME_ORG_LOCK);
69         server_lock_org = zebra_lock_create (path, 1);
70         if (!server_lock_org)
71         {
72             yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
73             return -1;
74         }
75     }
76     if (commitPhase)
77         h = server_lock_cmt;
78     else
79         h = server_lock_org;
80     if (zebra_lock_nb (h))
81     {
82 #ifndef WIN32
83         if (errno != EWOULDBLOCK)
84         {
85             yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock");
86             exit (1);
87         }
88 #endif
89         if (commitPhase)
90             yaz_log (YLOG_LOG, "Waiting for lock cmt");
91         else
92             yaz_log (YLOG_LOG, "Waiting for lock org");
93         if (zebra_lock (h) == -1)
94         {
95             yaz_log (YLOG_FATAL, "flock");
96             exit (1);
97         }
98     }
99     zebra_unlock (h);
100     return 0;
101 }
102
103
104 void zebraIndexLockMsg (ZebraHandle zh, const char *str)
105 {
106     char path[1024];
107     int l, r, fd;
108
109     assert (server_lock_main);
110     fd = zebra_lock_fd (server_lock_main);
111     lseek (fd, 0L, SEEK_SET);
112     l = strlen(str);
113     r = write (fd, str, l);
114     if (r != l)
115     {
116         yaz_log (YLOG_FATAL|YLOG_ERRNO, "write lock file");
117         exit (1);
118     }
119     zebra_lock_prefix (zh->service->res, path);
120     strcat (path, FNAME_TOUCH_TIME);
121     fd = creat (path, 0666);
122     close (fd);
123 }
124
125 void zebraIndexUnlock (ZebraHandle zh)
126 {
127     char path[1024];
128     
129     zebra_lock_prefix (zh->service->res, path);
130     strcat (path, FNAME_MAIN_LOCK);
131 #ifdef WIN32
132     zebra_lock_destroy (server_lock_main);
133     if (unlink (path) && errno != ENOENT)
134         yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
135 #else
136     if (unlink (path) && errno != ENOENT)
137         yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
138     zebra_lock_destroy (server_lock_main);
139 #endif
140     server_lock_main = 0;
141 }
142
143 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow,
144                     const char *rval)
145 {
146     char path[1024];
147     char buf[256];
148     int r;
149
150     if (server_lock_main)
151         return 0;
152
153     zebra_lock_prefix (zh->service->res, path);
154     strcat (path, FNAME_MAIN_LOCK);
155     while (1)
156     {
157         server_lock_main = zebra_lock_create (path, 2);
158         if (!server_lock_main)
159         {
160             server_lock_main = zebra_lock_create (path, 1);
161             if (!server_lock_main)
162             {
163                 yaz_log (YLOG_FATAL, "couldn't obtain indexer lock");
164                 exit (1);
165             }
166             if (zebra_lock_nb (server_lock_main) == -1)
167             {
168 #ifdef WIN32
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);
173                 continue;
174 #else
175                 if (errno == EWOULDBLOCK)
176                 {
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);
181                     continue;
182                 }
183                 else
184                 {
185                     yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock %s", path);
186                     exit (1);
187                 }
188 #endif
189             }
190             else
191             {
192                 int fd = zebra_lock_fd (server_lock_main);
193
194                 yaz_log (YLOG_WARN, "unlocked %s", path);
195                 r = read (fd, buf, 256);
196                 if (r == 0)
197                 {
198                     yaz_log (YLOG_WARN, "zero length %s", path);
199                     zebra_lock_destroy (server_lock_main);
200                     unlink (path);
201                     continue;
202                 }
203                 else if (r == -1)
204                 {
205                     yaz_log (YLOG_FATAL|YLOG_ERRNO, "read %s", path);
206                     exit (1);
207                 }
208                 if (*buf == 'r')
209                 {
210                     yaz_log (YLOG_WARN, "previous transaction didn't"
211                           " reach commit");
212                     zebra_lock_destroy (server_lock_main);
213                     bf_commitClean (bfs, rval);
214                     unlink (path);
215                     continue;
216                 }
217                 else if (*buf == 'd')
218                 {
219                     yaz_log (YLOG_WARN, "commit file wan't deleted after commit");
220                     zebra_lock_destroy (server_lock_main);
221                     bf_commitClean (bfs, rval);
222                     unlink (path);
223                     continue;
224                 }                    
225                 else if (*buf == 'w')
226                 {
227                     yaz_log (YLOG_WARN,
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);
233                     exit (1);
234                 }
235                 else if (*buf == 'c')
236                 {
237                     if (commitNow)
238                     {
239                         unlink (path);
240                         zebra_lock_destroy (server_lock_main);
241                         continue;
242                     }
243                     yaz_log (YLOG_FATAL, "previous transaction didn't"
244                           " finish commit. Commit now!");
245                     exit (1);
246                 }
247                 else 
248                 {
249                     yaz_log (YLOG_FATAL, "unknown id 0x%02x in %s", *buf,
250                           path);
251                     exit (1);
252                 }
253             }
254         }
255         else
256             break;
257     }
258     zebra_lock (server_lock_main);
259     return 0;
260 }
261