Statistics
| Branch: | Tag: | Revision:

root / lib / jstore.py @ 38e250ba

History | View | Annotate | Download (3.8 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 logging
26 8b537bb0 Michael Hanselmann
import errno
27 8b537bb0 Michael Hanselmann
import re
28 8b537bb0 Michael Hanselmann
29 8b537bb0 Michael Hanselmann
from ganeti import constants
30 8b537bb0 Michael Hanselmann
from ganeti import errors
31 8b537bb0 Michael Hanselmann
from ganeti import utils
32 8b537bb0 Michael Hanselmann
33 8b537bb0 Michael Hanselmann
34 8b537bb0 Michael Hanselmann
def _ReadNumericFile(file_name):
35 8b537bb0 Michael Hanselmann
  """Reads a file containing a number.
36 8b537bb0 Michael Hanselmann

37 8b537bb0 Michael Hanselmann
  @rtype: None or int
38 8b537bb0 Michael Hanselmann
  @return: None if file is not found, otherwise number
39 8b537bb0 Michael Hanselmann

40 8b537bb0 Michael Hanselmann
  """
41 8b537bb0 Michael Hanselmann
  try:
42 8b537bb0 Michael Hanselmann
    fd = open(file_name, "r")
43 8b537bb0 Michael Hanselmann
  except IOError, err:
44 8b537bb0 Michael Hanselmann
    if err.errno in (errno.ENOENT, ):
45 8b537bb0 Michael Hanselmann
      return None
46 8b537bb0 Michael Hanselmann
    raise
47 8b537bb0 Michael Hanselmann
48 8b537bb0 Michael Hanselmann
  try:
49 8b537bb0 Michael Hanselmann
    return int(fd.read(128))
50 8b537bb0 Michael Hanselmann
  finally:
51 8b537bb0 Michael Hanselmann
    fd.close()
52 8b537bb0 Michael Hanselmann
53 8b537bb0 Michael Hanselmann
54 8b537bb0 Michael Hanselmann
def ReadSerial():
55 8b537bb0 Michael Hanselmann
  """Read the serial file.
56 8b537bb0 Michael Hanselmann

57 8b537bb0 Michael Hanselmann
  The queue should be locked while this function is called.
58 8b537bb0 Michael Hanselmann

59 8b537bb0 Michael Hanselmann
  """
60 8b537bb0 Michael Hanselmann
  return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
61 8b537bb0 Michael Hanselmann
62 8b537bb0 Michael Hanselmann
63 8b537bb0 Michael Hanselmann
def ReadVersion():
64 8b537bb0 Michael Hanselmann
  """Read the queue version.
65 8b537bb0 Michael Hanselmann

66 8b537bb0 Michael Hanselmann
  The queue should be locked while this function is called.
67 8b537bb0 Michael Hanselmann

68 8b537bb0 Michael Hanselmann
  """
69 8b537bb0 Michael Hanselmann
  return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
70 8b537bb0 Michael Hanselmann
71 8b537bb0 Michael Hanselmann
72 5d6fb8eb Michael Hanselmann
def InitAndVerifyQueue(must_lock):
73 8b537bb0 Michael Hanselmann
  """Open and lock job queue.
74 8b537bb0 Michael Hanselmann

75 8b537bb0 Michael Hanselmann
  If necessary, the queue is automatically initialized.
76 8b537bb0 Michael Hanselmann

77 5d6fb8eb Michael Hanselmann
  @type must_lock: bool
78 5d6fb8eb Michael Hanselmann
  @param must_lock: Whether an exclusive lock must be held.
79 8b537bb0 Michael Hanselmann
  @rtype: utils.FileLock
80 8b537bb0 Michael Hanselmann
  @return: Lock object for the queue. This can be used to change the
81 8b537bb0 Michael Hanselmann
           locking mode.
82 8b537bb0 Michael Hanselmann

83 8b537bb0 Michael Hanselmann
  """
84 8b537bb0 Michael Hanselmann
  # Make sure our directories exists
85 8b537bb0 Michael Hanselmann
  for path in (constants.QUEUE_DIR, constants.JOB_QUEUE_ARCHIVE_DIR):
86 8b537bb0 Michael Hanselmann
    try:
87 8b537bb0 Michael Hanselmann
      os.mkdir(path, 0700)
88 8b537bb0 Michael Hanselmann
    except OSError, err:
89 8b537bb0 Michael Hanselmann
      if err.errno not in (errno.EEXIST, ):
90 8b537bb0 Michael Hanselmann
        raise
91 8b537bb0 Michael Hanselmann
92 8b537bb0 Michael Hanselmann
  # Lock queue
93 8b537bb0 Michael Hanselmann
  queue_lock = utils.FileLock(constants.JOB_QUEUE_LOCK_FILE)
94 8b537bb0 Michael Hanselmann
  try:
95 5d6fb8eb Michael Hanselmann
    # The queue needs to be locked in exclusive mode to write to the serial and
96 5d6fb8eb Michael Hanselmann
    # version files.
97 5d6fb8eb Michael Hanselmann
    if must_lock:
98 5d6fb8eb Michael Hanselmann
      queue_lock.Exclusive(blocking=True)
99 5d6fb8eb Michael Hanselmann
      holding_lock = True
100 8b537bb0 Michael Hanselmann
    else:
101 5d6fb8eb Michael Hanselmann
      try:
102 5d6fb8eb Michael Hanselmann
        queue_lock.Exclusive(blocking=False)
103 5d6fb8eb Michael Hanselmann
        holding_lock = True
104 5d6fb8eb Michael Hanselmann
      except errors.LockError:
105 5d6fb8eb Michael Hanselmann
        # Ignore errors and assume the process keeping the lock checked
106 5d6fb8eb Michael Hanselmann
        # everything.
107 5d6fb8eb Michael Hanselmann
        holding_lock = False
108 5d6fb8eb Michael Hanselmann
109 5d6fb8eb Michael Hanselmann
    if holding_lock:
110 5d6fb8eb Michael Hanselmann
      # Verify version
111 8b537bb0 Michael Hanselmann
      version = ReadVersion()
112 5d6fb8eb Michael Hanselmann
      if version is None:
113 5d6fb8eb Michael Hanselmann
        # Write new version file
114 5d6fb8eb Michael Hanselmann
        utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE,
115 5d6fb8eb Michael Hanselmann
                        data="%s\n" % constants.JOB_QUEUE_VERSION)
116 8b537bb0 Michael Hanselmann
117 5d6fb8eb Michael Hanselmann
        # Read again
118 5d6fb8eb Michael Hanselmann
        version = ReadVersion()
119 8b537bb0 Michael Hanselmann
120 5d6fb8eb Michael Hanselmann
      if version != constants.JOB_QUEUE_VERSION:
121 5d6fb8eb Michael Hanselmann
        raise errors.JobQueueError("Found job queue version %s, expected %s",
122 5d6fb8eb Michael Hanselmann
                                   version, constants.JOB_QUEUE_VERSION)
123 8b537bb0 Michael Hanselmann
124 8b537bb0 Michael Hanselmann
      serial = ReadSerial()
125 5d6fb8eb Michael Hanselmann
      if serial is None:
126 5d6fb8eb Michael Hanselmann
        # Write new serial file
127 5d6fb8eb Michael Hanselmann
        utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE,
128 5d6fb8eb Michael Hanselmann
                        data="%s\n" % 0)
129 5d6fb8eb Michael Hanselmann
130 5d6fb8eb Michael Hanselmann
        # Read again
131 5d6fb8eb Michael Hanselmann
        serial = ReadSerial()
132 8b537bb0 Michael Hanselmann
133 5d6fb8eb Michael Hanselmann
      if serial is None:
134 5d6fb8eb Michael Hanselmann
        # There must be a serious problem
135 5d6fb8eb Michael Hanselmann
        raise errors.JobQueueError("Can't read/parse the job queue serial file")
136 8b537bb0 Michael Hanselmann
137 f56377a3 Michael Hanselmann
      if not must_lock:
138 f56377a3 Michael Hanselmann
        # There's no need for more error handling. Closing the lock file below in
139 f56377a3 Michael Hanselmann
        # case of an error will unlock it anyway.
140 f56377a3 Michael Hanselmann
        queue_lock.Unlock()
141 f56377a3 Michael Hanselmann
142 8b537bb0 Michael Hanselmann
  except:
143 8b537bb0 Michael Hanselmann
    queue_lock.Close()
144 8b537bb0 Michael Hanselmann
    raise
145 8b537bb0 Michael Hanselmann
146 8b537bb0 Michael Hanselmann
  return queue_lock