root / lib / jstore.py @ 936f3c59
History | View | Annotate | Download (3.7 kB)
1 | 8b537bb0 | Michael Hanselmann | #
|
---|---|---|---|
2 | 8b537bb0 | Michael Hanselmann | #
|
3 | 8b537bb0 | Michael Hanselmann | |
4 | 8b537bb0 | Michael Hanselmann | # Copyright (C) 2006, 2007 Google Inc.
|
5 | 8b537bb0 | Michael Hanselmann | #
|
6 | 8b537bb0 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 8b537bb0 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 8b537bb0 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 8b537bb0 | Michael Hanselmann | # (at your option) any later version.
|
10 | 8b537bb0 | Michael Hanselmann | #
|
11 | 8b537bb0 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 8b537bb0 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 8b537bb0 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 8b537bb0 | Michael Hanselmann | # General Public License for more details.
|
15 | 8b537bb0 | Michael Hanselmann | #
|
16 | 8b537bb0 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 8b537bb0 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 8b537bb0 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 8b537bb0 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 8b537bb0 | Michael Hanselmann | |
21 | 8b537bb0 | Michael Hanselmann | |
22 | 8b537bb0 | Michael Hanselmann | """Module implementing the job queue handling."""
|
23 | 8b537bb0 | Michael Hanselmann | |
24 | 8b537bb0 | Michael Hanselmann | import os |
25 | 8b537bb0 | Michael Hanselmann | import errno |
26 | 8b537bb0 | Michael Hanselmann | |
27 | 8b537bb0 | Michael Hanselmann | from ganeti import constants |
28 | 8b537bb0 | Michael Hanselmann | from ganeti import errors |
29 | 8b537bb0 | Michael Hanselmann | from ganeti import utils |
30 | 8b537bb0 | Michael Hanselmann | |
31 | 8b537bb0 | Michael Hanselmann | |
32 | 8b537bb0 | Michael Hanselmann | def _ReadNumericFile(file_name): |
33 | 8b537bb0 | Michael Hanselmann | """Reads a file containing a number.
|
34 | 8b537bb0 | Michael Hanselmann |
|
35 | 8b537bb0 | Michael Hanselmann | @rtype: None or int
|
36 | 8b537bb0 | Michael Hanselmann | @return: None if file is not found, otherwise number
|
37 | 8b537bb0 | Michael Hanselmann |
|
38 | 8b537bb0 | Michael Hanselmann | """
|
39 | 8b537bb0 | Michael Hanselmann | try:
|
40 | 8b537bb0 | Michael Hanselmann | fd = open(file_name, "r") |
41 | 8b537bb0 | Michael Hanselmann | except IOError, err: |
42 | 8b537bb0 | Michael Hanselmann | if err.errno in (errno.ENOENT, ): |
43 | 8b537bb0 | Michael Hanselmann | return None |
44 | 8b537bb0 | Michael Hanselmann | raise
|
45 | 8b537bb0 | Michael Hanselmann | |
46 | 8b537bb0 | Michael Hanselmann | try:
|
47 | 8b537bb0 | Michael Hanselmann | return int(fd.read(128)) |
48 | 8b537bb0 | Michael Hanselmann | finally:
|
49 | 8b537bb0 | Michael Hanselmann | fd.close() |
50 | 8b537bb0 | Michael Hanselmann | |
51 | 8b537bb0 | Michael Hanselmann | |
52 | 8b537bb0 | Michael Hanselmann | def ReadSerial(): |
53 | 8b537bb0 | Michael Hanselmann | """Read the serial file.
|
54 | 8b537bb0 | Michael Hanselmann |
|
55 | 8b537bb0 | Michael Hanselmann | The queue should be locked while this function is called.
|
56 | 8b537bb0 | Michael Hanselmann |
|
57 | 8b537bb0 | Michael Hanselmann | """
|
58 | 8b537bb0 | Michael Hanselmann | return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
|
59 | 8b537bb0 | Michael Hanselmann | |
60 | 8b537bb0 | Michael Hanselmann | |
61 | 8b537bb0 | Michael Hanselmann | def ReadVersion(): |
62 | 8b537bb0 | Michael Hanselmann | """Read the queue version.
|
63 | 8b537bb0 | Michael Hanselmann |
|
64 | 8b537bb0 | Michael Hanselmann | The queue should be locked while this function is called.
|
65 | 8b537bb0 | Michael Hanselmann |
|
66 | 8b537bb0 | Michael Hanselmann | """
|
67 | 8b537bb0 | Michael Hanselmann | return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
|
68 | 8b537bb0 | Michael Hanselmann | |
69 | 8b537bb0 | Michael Hanselmann | |
70 | 5d6fb8eb | Michael Hanselmann | def InitAndVerifyQueue(must_lock): |
71 | 8b537bb0 | Michael Hanselmann | """Open and lock job queue.
|
72 | 8b537bb0 | Michael Hanselmann |
|
73 | 8b537bb0 | Michael Hanselmann | If necessary, the queue is automatically initialized.
|
74 | 8b537bb0 | Michael Hanselmann |
|
75 | 5d6fb8eb | Michael Hanselmann | @type must_lock: bool
|
76 | 5d6fb8eb | Michael Hanselmann | @param must_lock: Whether an exclusive lock must be held.
|
77 | 8b537bb0 | Michael Hanselmann | @rtype: utils.FileLock
|
78 | 8b537bb0 | Michael Hanselmann | @return: Lock object for the queue. This can be used to change the
|
79 | 8b537bb0 | Michael Hanselmann | locking mode.
|
80 | 8b537bb0 | Michael Hanselmann |
|
81 | 8b537bb0 | Michael Hanselmann | """
|
82 | 8b537bb0 | Michael Hanselmann | # Make sure our directories exists
|
83 | 8b537bb0 | Michael Hanselmann | for path in (constants.QUEUE_DIR, constants.JOB_QUEUE_ARCHIVE_DIR): |
84 | 8b537bb0 | Michael Hanselmann | try:
|
85 | 8b537bb0 | Michael Hanselmann | os.mkdir(path, 0700)
|
86 | 8b537bb0 | Michael Hanselmann | except OSError, err: |
87 | 8b537bb0 | Michael Hanselmann | if err.errno not in (errno.EEXIST, ): |
88 | 8b537bb0 | Michael Hanselmann | raise
|
89 | 8b537bb0 | Michael Hanselmann | |
90 | 8b537bb0 | Michael Hanselmann | # Lock queue
|
91 | 8b537bb0 | Michael Hanselmann | queue_lock = utils.FileLock(constants.JOB_QUEUE_LOCK_FILE) |
92 | 8b537bb0 | Michael Hanselmann | try:
|
93 | 5d6fb8eb | Michael Hanselmann | # The queue needs to be locked in exclusive mode to write to the serial and
|
94 | 5d6fb8eb | Michael Hanselmann | # version files.
|
95 | 5d6fb8eb | Michael Hanselmann | if must_lock:
|
96 | 5d6fb8eb | Michael Hanselmann | queue_lock.Exclusive(blocking=True)
|
97 | 5d6fb8eb | Michael Hanselmann | holding_lock = True
|
98 | 8b537bb0 | Michael Hanselmann | else:
|
99 | 5d6fb8eb | Michael Hanselmann | try:
|
100 | 5d6fb8eb | Michael Hanselmann | queue_lock.Exclusive(blocking=False)
|
101 | 5d6fb8eb | Michael Hanselmann | holding_lock = True
|
102 | 5d6fb8eb | Michael Hanselmann | except errors.LockError:
|
103 | 5d6fb8eb | Michael Hanselmann | # Ignore errors and assume the process keeping the lock checked
|
104 | 5d6fb8eb | Michael Hanselmann | # everything.
|
105 | 5d6fb8eb | Michael Hanselmann | holding_lock = False
|
106 | 5d6fb8eb | Michael Hanselmann | |
107 | 5d6fb8eb | Michael Hanselmann | if holding_lock:
|
108 | 5d6fb8eb | Michael Hanselmann | # Verify version
|
109 | 8b537bb0 | Michael Hanselmann | version = ReadVersion() |
110 | 5d6fb8eb | Michael Hanselmann | if version is None: |
111 | 5d6fb8eb | Michael Hanselmann | # Write new version file
|
112 | 5d6fb8eb | Michael Hanselmann | utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE, |
113 | 5d6fb8eb | Michael Hanselmann | data="%s\n" % constants.JOB_QUEUE_VERSION)
|
114 | 8b537bb0 | Michael Hanselmann | |
115 | 5d6fb8eb | Michael Hanselmann | # Read again
|
116 | 5d6fb8eb | Michael Hanselmann | version = ReadVersion() |
117 | 8b537bb0 | Michael Hanselmann | |
118 | 5d6fb8eb | Michael Hanselmann | if version != constants.JOB_QUEUE_VERSION:
|
119 | 5d6fb8eb | Michael Hanselmann | raise errors.JobQueueError("Found job queue version %s, expected %s", |
120 | 5d6fb8eb | Michael Hanselmann | version, constants.JOB_QUEUE_VERSION) |
121 | 8b537bb0 | Michael Hanselmann | |
122 | 8b537bb0 | Michael Hanselmann | serial = ReadSerial() |
123 | 5d6fb8eb | Michael Hanselmann | if serial is None: |
124 | 5d6fb8eb | Michael Hanselmann | # Write new serial file
|
125 | 5d6fb8eb | Michael Hanselmann | utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE, |
126 | 5d6fb8eb | Michael Hanselmann | data="%s\n" % 0) |
127 | 5d6fb8eb | Michael Hanselmann | |
128 | 5d6fb8eb | Michael Hanselmann | # Read again
|
129 | 5d6fb8eb | Michael Hanselmann | serial = ReadSerial() |
130 | 8b537bb0 | Michael Hanselmann | |
131 | 5d6fb8eb | Michael Hanselmann | if serial is None: |
132 | 5d6fb8eb | Michael Hanselmann | # There must be a serious problem
|
133 | 5d6fb8eb | Michael Hanselmann | raise errors.JobQueueError("Can't read/parse the job queue serial file") |
134 | 8b537bb0 | Michael Hanselmann | |
135 | f56377a3 | Michael Hanselmann | if not must_lock: |
136 | 4d4a651d | Michael Hanselmann | # There's no need for more error handling. Closing the lock file below
|
137 | 4d4a651d | Michael Hanselmann | # in case of an error will unlock it anyway.
|
138 | f56377a3 | Michael Hanselmann | queue_lock.Unlock() |
139 | f56377a3 | Michael Hanselmann | |
140 | 8b537bb0 | Michael Hanselmann | except:
|
141 | 8b537bb0 | Michael Hanselmann | queue_lock.Close() |
142 | 8b537bb0 | Michael Hanselmann | raise
|
143 | 8b537bb0 | Michael Hanselmann | |
144 | 8b537bb0 | Michael Hanselmann | return queue_lock |