root / src / Ganeti / Query / Server.hs @ 178ad717
History | View | Annotate | Download (16.7 kB)
1 | d120506c | Agata Murawska | {-| Implementation of the Ganeti Query2 server. |
---|---|---|---|
2 | 25b54de0 | Iustin Pop | |
3 | 25b54de0 | Iustin Pop | -} |
4 | 25b54de0 | Iustin Pop | |
5 | 25b54de0 | Iustin Pop | {- |
6 | 25b54de0 | Iustin Pop | |
7 | 72747d91 | Iustin Pop | Copyright (C) 2012, 2013 Google Inc. |
8 | 25b54de0 | Iustin Pop | |
9 | 25b54de0 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
10 | 25b54de0 | Iustin Pop | it under the terms of the GNU General Public License as published by |
11 | 25b54de0 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
12 | 25b54de0 | Iustin Pop | (at your option) any later version. |
13 | 25b54de0 | Iustin Pop | |
14 | 25b54de0 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
15 | 25b54de0 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 25b54de0 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | 25b54de0 | Iustin Pop | General Public License for more details. |
18 | 25b54de0 | Iustin Pop | |
19 | 25b54de0 | Iustin Pop | You should have received a copy of the GNU General Public License |
20 | 25b54de0 | Iustin Pop | along with this program; if not, write to the Free Software |
21 | 25b54de0 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 25b54de0 | Iustin Pop | 02110-1301, USA. |
23 | 25b54de0 | Iustin Pop | |
24 | 25b54de0 | Iustin Pop | -} |
25 | 25b54de0 | Iustin Pop | |
26 | 4cab6703 | Iustin Pop | module Ganeti.Query.Server |
27 | 670e954a | Thomas Thrainer | ( main |
28 | 670e954a | Thomas Thrainer | , checkMain |
29 | 670e954a | Thomas Thrainer | , prepMain |
30 | 0d0ac025 | Iustin Pop | ) where |
31 | 25b54de0 | Iustin Pop | |
32 | f2374060 | Iustin Pop | import Control.Applicative |
33 | 25b54de0 | Iustin Pop | import Control.Concurrent |
34 | 25b54de0 | Iustin Pop | import Control.Exception |
35 | c6013594 | Klaus Aehlig | import Control.Monad (forever, when, zipWithM, liftM) |
36 | 25b54de0 | Iustin Pop | import Data.Bits (bitSize) |
37 | c87997d2 | Jose A. Lopes | import qualified Data.Set as Set (toList) |
38 | 670e954a | Thomas Thrainer | import Data.IORef |
39 | be7531a9 | Klaus Aehlig | import Data.Maybe (fromMaybe) |
40 | 25b54de0 | Iustin Pop | import qualified Text.JSON as J |
41 | 6222b3a3 | Klaus Aehlig | import Text.JSON (encode, showJSON, JSValue(..)) |
42 | 25b54de0 | Iustin Pop | import System.Info (arch) |
43 | d5665e10 | Klaus Aehlig | import System.Directory |
44 | 25b54de0 | Iustin Pop | |
45 | 25b54de0 | Iustin Pop | import qualified Ganeti.Constants as C |
46 | c87997d2 | Jose A. Lopes | import qualified Ganeti.ConstantUtils as ConstantUtils (unFrozenSet) |
47 | 5183e8be | Iustin Pop | import Ganeti.Errors |
48 | 9eeb0aa5 | Michael Hanselmann | import qualified Ganeti.Path as Path |
49 | 0d0ac025 | Iustin Pop | import Ganeti.Daemon |
50 | 25b54de0 | Iustin Pop | import Ganeti.Objects |
51 | f2374060 | Iustin Pop | import qualified Ganeti.Config as Config |
52 | 218e3b0f | Thomas Thrainer | import Ganeti.ConfigReader |
53 | 25b54de0 | Iustin Pop | import Ganeti.BasicTypes |
54 | e5fba493 | Klaus Aehlig | import Ganeti.JQueue |
55 | b5fa2700 | Klaus Aehlig | import Ganeti.JQScheduler |
56 | 25b54de0 | Iustin Pop | import Ganeti.Logging |
57 | 25b54de0 | Iustin Pop | import Ganeti.Luxi |
58 | 4cab6703 | Iustin Pop | import qualified Ganeti.Query.Language as Qlang |
59 | 1c3231aa | Thomas Thrainer | import qualified Ganeti.Query.Cluster as QCluster |
60 | d5665e10 | Klaus Aehlig | import Ganeti.Path (queueDir, jobQueueLockFile, jobQueueDrainFile) |
61 | 946f1fb3 | Klaus Aehlig | import Ganeti.Rpc |
62 | 4cbe9bda | Iustin Pop | import Ganeti.Query.Query |
63 | c4e0d065 | Klaus Aehlig | import Ganeti.Query.Filter (makeSimpleFilter) |
64 | 6e94b75c | Jose A. Lopes | import Ganeti.Types |
65 | c7003a76 | Petr Pudlak | import qualified Ganeti.UDSServer as U (Handler(..), listener) |
66 | 6222b3a3 | Klaus Aehlig | import Ganeti.Utils (lockFile, exitIfBad, watchFile) |
67 | 3cb9bd38 | Jose A. Lopes | import qualified Ganeti.Version as Version |
68 | 25b54de0 | Iustin Pop | |
69 | cd67e337 | Iustin Pop | -- | Helper for classic queries. |
70 | cd67e337 | Iustin Pop | handleClassicQuery :: ConfigData -- ^ Cluster config |
71 | cd67e337 | Iustin Pop | -> Qlang.ItemType -- ^ Query type |
72 | 037762a9 | Iustin Pop | -> [Either String Integer] -- ^ Requested names |
73 | 037762a9 | Iustin Pop | -- (empty means all) |
74 | cd67e337 | Iustin Pop | -> [String] -- ^ Requested fields |
75 | cd67e337 | Iustin Pop | -> Bool -- ^ Whether to do sync queries or not |
76 | 5183e8be | Iustin Pop | -> IO (GenericResult GanetiException JSValue) |
77 | c4e0d065 | Klaus Aehlig | handleClassicQuery _ _ _ _ True = |
78 | 5183e8be | Iustin Pop | return . Bad $ OpPrereqError "Sync queries are not allowed" ECodeInval |
79 | c4e0d065 | Klaus Aehlig | handleClassicQuery cfg qkind names fields _ = do |
80 | c4e0d065 | Klaus Aehlig | let flt = makeSimpleFilter (nameField qkind) names |
81 | cd67e337 | Iustin Pop | qr <- query cfg True (Qlang.Query qkind fields flt) |
82 | cd67e337 | Iustin Pop | return $ showJSON <$> (qr >>= queryCompat) |
83 | cd67e337 | Iustin Pop | |
84 | 25b54de0 | Iustin Pop | -- | Minimal wrapper to handle the missing config case. |
85 | 9131274c | Jose A. Lopes | handleCallWrapper :: MVar () -> JQStatus -> Result ConfigData |
86 | e5fba493 | Klaus Aehlig | -> LuxiOp -> IO (ErrorResult JSValue) |
87 | b5fa2700 | Klaus Aehlig | handleCallWrapper _ _ (Bad msg) _ = |
88 | 5183e8be | Iustin Pop | return . Bad . ConfigurationError $ |
89 | 5183e8be | Iustin Pop | "I do not have access to a valid configuration, cannot\ |
90 | 5183e8be | Iustin Pop | \ process queries: " ++ msg |
91 | b5fa2700 | Klaus Aehlig | handleCallWrapper qlock qstat (Ok config) op = handleCall qlock qstat config op |
92 | 25b54de0 | Iustin Pop | |
93 | 25b54de0 | Iustin Pop | -- | Actual luxi operation handler. |
94 | 9131274c | Jose A. Lopes | handleCall :: MVar () -> JQStatus |
95 | b5fa2700 | Klaus Aehlig | -> ConfigData -> LuxiOp -> IO (ErrorResult JSValue) |
96 | b5fa2700 | Klaus Aehlig | handleCall _ _ cdata QueryClusterInfo = |
97 | 25b54de0 | Iustin Pop | let cluster = configCluster cdata |
98 | 1c3231aa | Thomas Thrainer | master = QCluster.clusterMasterNodeName cdata |
99 | 25b54de0 | Iustin Pop | hypervisors = clusterEnabledHypervisors cluster |
100 | 966e1580 | Helga Velroyen | diskTemplates = clusterEnabledDiskTemplates cluster |
101 | 72747d91 | Iustin Pop | def_hv = case hypervisors of |
102 | 72747d91 | Iustin Pop | x:_ -> showJSON x |
103 | 72747d91 | Iustin Pop | [] -> JSNull |
104 | 25b54de0 | Iustin Pop | bits = show (bitSize (0::Int)) ++ "bits" |
105 | 25b54de0 | Iustin Pop | arch_tuple = [bits, arch] |
106 | 5b11f8db | Iustin Pop | obj = [ ("software_version", showJSON C.releaseVersion) |
107 | 5b11f8db | Iustin Pop | , ("protocol_version", showJSON C.protocolVersion) |
108 | 5b11f8db | Iustin Pop | , ("config_version", showJSON C.configVersion) |
109 | c87997d2 | Jose A. Lopes | , ("os_api_version", showJSON . maximum . |
110 | c87997d2 | Jose A. Lopes | Set.toList . ConstantUtils.unFrozenSet $ |
111 | c87997d2 | Jose A. Lopes | C.osApiVersions) |
112 | 5b11f8db | Iustin Pop | , ("export_version", showJSON C.exportVersion) |
113 | 3cb9bd38 | Jose A. Lopes | , ("vcs_version", showJSON Version.version) |
114 | 5b11f8db | Iustin Pop | , ("architecture", showJSON arch_tuple) |
115 | 25b54de0 | Iustin Pop | , ("name", showJSON $ clusterClusterName cluster) |
116 | 1c3231aa | Thomas Thrainer | , ("master", showJSON (case master of |
117 | 1c3231aa | Thomas Thrainer | Ok name -> name |
118 | 1c3231aa | Thomas Thrainer | _ -> undefined)) |
119 | 72747d91 | Iustin Pop | , ("default_hypervisor", def_hv) |
120 | 5b11f8db | Iustin Pop | , ("enabled_hypervisors", showJSON hypervisors) |
121 | a2160e57 | Iustin Pop | , ("hvparams", showJSON $ clusterHvparams cluster) |
122 | a2160e57 | Iustin Pop | , ("os_hvp", showJSON $ clusterOsHvp cluster) |
123 | 25b54de0 | Iustin Pop | , ("beparams", showJSON $ clusterBeparams cluster) |
124 | 25b54de0 | Iustin Pop | , ("osparams", showJSON $ clusterOsparams cluster) |
125 | 25b54de0 | Iustin Pop | , ("ipolicy", showJSON $ clusterIpolicy cluster) |
126 | 25b54de0 | Iustin Pop | , ("nicparams", showJSON $ clusterNicparams cluster) |
127 | 25b54de0 | Iustin Pop | , ("ndparams", showJSON $ clusterNdparams cluster) |
128 | a2160e57 | Iustin Pop | , ("diskparams", showJSON $ clusterDiskparams cluster) |
129 | 25b54de0 | Iustin Pop | , ("candidate_pool_size", |
130 | 25b54de0 | Iustin Pop | showJSON $ clusterCandidatePoolSize cluster) |
131 | 178ad717 | Klaus Aehlig | , ("max_running_jobs", |
132 | 178ad717 | Klaus Aehlig | showJSON $ clusterMaxRunningJobs cluster) |
133 | 25b54de0 | Iustin Pop | , ("master_netdev", showJSON $ clusterMasterNetdev cluster) |
134 | 25b54de0 | Iustin Pop | , ("master_netmask", showJSON $ clusterMasterNetmask cluster) |
135 | 25b54de0 | Iustin Pop | , ("use_external_mip_script", |
136 | 25b54de0 | Iustin Pop | showJSON $ clusterUseExternalMipScript cluster) |
137 | 64b0309a | Dimitris Aragiorgis | , ("volume_group_name", |
138 | 64b0309a | Dimitris Aragiorgis | maybe JSNull showJSON (clusterVolumeGroupName cluster)) |
139 | 25b54de0 | Iustin Pop | , ("drbd_usermode_helper", |
140 | 25b54de0 | Iustin Pop | maybe JSNull showJSON (clusterDrbdUsermodeHelper cluster)) |
141 | 25b54de0 | Iustin Pop | , ("file_storage_dir", showJSON $ clusterFileStorageDir cluster) |
142 | 25b54de0 | Iustin Pop | , ("shared_file_storage_dir", |
143 | 25b54de0 | Iustin Pop | showJSON $ clusterSharedFileStorageDir cluster) |
144 | d3e6fd0e | Santi Raffa | , ("gluster_storage_dir", |
145 | d3e6fd0e | Santi Raffa | showJSON $ clusterGlusterStorageDir cluster) |
146 | 25b54de0 | Iustin Pop | , ("maintain_node_health", |
147 | 25b54de0 | Iustin Pop | showJSON $ clusterMaintainNodeHealth cluster) |
148 | 25b54de0 | Iustin Pop | , ("ctime", showJSON $ clusterCtime cluster) |
149 | 25b54de0 | Iustin Pop | , ("mtime", showJSON $ clusterMtime cluster) |
150 | 25b54de0 | Iustin Pop | , ("uuid", showJSON $ clusterUuid cluster) |
151 | 25b54de0 | Iustin Pop | , ("tags", showJSON $ clusterTags cluster) |
152 | 25b54de0 | Iustin Pop | , ("uid_pool", showJSON $ clusterUidPool cluster) |
153 | 25b54de0 | Iustin Pop | , ("default_iallocator", |
154 | 25b54de0 | Iustin Pop | showJSON $ clusterDefaultIallocator cluster) |
155 | 0359e5d0 | Spyros Trigazis | , ("default_iallocator_params", |
156 | 0359e5d0 | Spyros Trigazis | showJSON $ clusterDefaultIallocatorParams cluster) |
157 | 25b54de0 | Iustin Pop | , ("reserved_lvs", showJSON $ clusterReservedLvs cluster) |
158 | 25b54de0 | Iustin Pop | , ("primary_ip_version", |
159 | 25b54de0 | Iustin Pop | showJSON . ipFamilyToVersion $ clusterPrimaryIpFamily cluster) |
160 | 7b9ceea7 | Helga Velroyen | , ("prealloc_wipe_disks", |
161 | 7b9ceea7 | Helga Velroyen | showJSON $ clusterPreallocWipeDisks cluster) |
162 | 7b9ceea7 | Helga Velroyen | , ("hidden_os", showJSON $ clusterHiddenOs cluster) |
163 | 7b9ceea7 | Helga Velroyen | , ("blacklisted_os", showJSON $ clusterBlacklistedOs cluster) |
164 | 966e1580 | Helga Velroyen | , ("enabled_disk_templates", showJSON diskTemplates) |
165 | 25b54de0 | Iustin Pop | ] |
166 | 25b54de0 | Iustin Pop | |
167 | 1c3231aa | Thomas Thrainer | in case master of |
168 | 1c3231aa | Thomas Thrainer | Ok _ -> return . Ok . J.makeObj $ obj |
169 | 1c3231aa | Thomas Thrainer | Bad ex -> return $ Bad ex |
170 | 25b54de0 | Iustin Pop | |
171 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (QueryTags kind name) = do |
172 | f2374060 | Iustin Pop | let tags = case kind of |
173 | 6e94b75c | Jose A. Lopes | TagKindCluster -> Ok . clusterTags $ configCluster cfg |
174 | 6e94b75c | Jose A. Lopes | TagKindGroup -> groupTags <$> Config.getGroup cfg name |
175 | 6e94b75c | Jose A. Lopes | TagKindNode -> nodeTags <$> Config.getNode cfg name |
176 | 6e94b75c | Jose A. Lopes | TagKindInstance -> instTags <$> Config.getInstance cfg name |
177 | a8633d70 | Jose A. Lopes | TagKindNetwork -> Bad $ OpPrereqError |
178 | a8633d70 | Jose A. Lopes | "Network tag is not allowed" |
179 | a8633d70 | Jose A. Lopes | ECodeInval |
180 | 6e94b75c | Jose A. Lopes | return (J.showJSON <$> tags) |
181 | f2374060 | Iustin Pop | |
182 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (Query qkind qfields qfilter) = do |
183 | fa2c927c | Agata Murawska | result <- query cfg True (Qlang.Query qkind qfields qfilter) |
184 | 4cbe9bda | Iustin Pop | return $ J.showJSON <$> result |
185 | 4cbe9bda | Iustin Pop | |
186 | b5fa2700 | Klaus Aehlig | handleCall _ _ _ (QueryFields qkind qfields) = do |
187 | 518023a9 | Iustin Pop | let result = queryFields (Qlang.QueryFields qkind qfields) |
188 | 518023a9 | Iustin Pop | return $ J.showJSON <$> result |
189 | 518023a9 | Iustin Pop | |
190 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (QueryNodes names fields lock) = |
191 | 037762a9 | Iustin Pop | handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRNode) |
192 | c4e0d065 | Klaus Aehlig | (map Left names) fields lock |
193 | cd67e337 | Iustin Pop | |
194 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (QueryInstances names fields lock) = |
195 | 89352544 | Helga Velroyen | handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRInstance) |
196 | 89352544 | Helga Velroyen | (map Left names) fields lock |
197 | 89352544 | Helga Velroyen | |
198 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (QueryGroups names fields lock) = |
199 | 037762a9 | Iustin Pop | handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRGroup) |
200 | c4e0d065 | Klaus Aehlig | (map Left names) fields lock |
201 | cd67e337 | Iustin Pop | |
202 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (QueryJobs names fields) = |
203 | a7e484c4 | Iustin Pop | handleClassicQuery cfg (Qlang.ItemTypeLuxi Qlang.QRJob) |
204 | c4e0d065 | Klaus Aehlig | (map (Right . fromIntegral . fromJobId) names) fields False |
205 | a7e484c4 | Iustin Pop | |
206 | b5fa2700 | Klaus Aehlig | handleCall _ _ cfg (QueryNetworks names fields lock) = |
207 | 795d035d | Klaus Aehlig | handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRNetwork) |
208 | 795d035d | Klaus Aehlig | (map Left names) fields lock |
209 | 795d035d | Klaus Aehlig | |
210 | be7531a9 | Klaus Aehlig | handleCall _ _ cfg (QueryConfigValues fields) = do |
211 | be7531a9 | Klaus Aehlig | let params = [ ("cluster_name", return . showJSON . clusterClusterName |
212 | be7531a9 | Klaus Aehlig | . configCluster $ cfg) |
213 | be7531a9 | Klaus Aehlig | , ("watcher_pause", liftM (maybe JSNull showJSON) |
214 | be7531a9 | Klaus Aehlig | QCluster.isWatcherPaused) |
215 | be7531a9 | Klaus Aehlig | , ("master_node", return . genericResult (const JSNull) showJSON |
216 | be7531a9 | Klaus Aehlig | $ QCluster.clusterMasterNodeName cfg) |
217 | 1264bd58 | Klaus Aehlig | , ("drain_flag", liftM (showJSON . not) isQueueOpen) |
218 | be7531a9 | Klaus Aehlig | ] :: [(String, IO JSValue)] |
219 | be7531a9 | Klaus Aehlig | let answer = map (fromMaybe (return JSNull) . flip lookup params) fields |
220 | be7531a9 | Klaus Aehlig | answerEval <- sequence answer |
221 | be7531a9 | Klaus Aehlig | return . Ok . showJSON $ answerEval |
222 | be7531a9 | Klaus Aehlig | |
223 | b5fa2700 | Klaus Aehlig | handleCall qlock qstat cfg (SubmitJobToDrainedQueue ops) = |
224 | e5fba493 | Klaus Aehlig | do |
225 | f5b765f0 | Klaus Aehlig | let mcs = Config.getMasterCandidates cfg |
226 | f5b765f0 | Klaus Aehlig | jobid <- allocateJobId mcs qlock |
227 | e5fba493 | Klaus Aehlig | case jobid of |
228 | e5fba493 | Klaus Aehlig | Bad s -> return . Bad . GenericError $ s |
229 | e5fba493 | Klaus Aehlig | Ok jid -> do |
230 | c6013594 | Klaus Aehlig | ts <- currentTimestamp |
231 | c6013594 | Klaus Aehlig | job <- liftM (setReceivedTimestamp ts) |
232 | c6013594 | Klaus Aehlig | $ queuedJobFromOpCodes jid ops |
233 | e5fba493 | Klaus Aehlig | qDir <- queueDir |
234 | e5fba493 | Klaus Aehlig | write_result <- writeJobToDisk qDir job |
235 | e5fba493 | Klaus Aehlig | case write_result of |
236 | e5fba493 | Klaus Aehlig | Bad s -> return . Bad . GenericError $ s |
237 | e5fba493 | Klaus Aehlig | Ok () -> do |
238 | f5b765f0 | Klaus Aehlig | _ <- replicateManyJobs qDir mcs [job] |
239 | b5fa2700 | Klaus Aehlig | _ <- forkIO $ enqueueNewJobs qstat [job] |
240 | e5fba493 | Klaus Aehlig | return . Ok . showJSON . fromJobId $ jid |
241 | e5fba493 | Klaus Aehlig | |
242 | b5fa2700 | Klaus Aehlig | handleCall qlock qstat cfg (SubmitJob ops) = |
243 | e5fba493 | Klaus Aehlig | do |
244 | e5fba493 | Klaus Aehlig | open <- isQueueOpen |
245 | e5fba493 | Klaus Aehlig | if not open |
246 | e5fba493 | Klaus Aehlig | then return . Bad . GenericError $ "Queue drained" |
247 | b5fa2700 | Klaus Aehlig | else handleCall qlock qstat cfg (SubmitJobToDrainedQueue ops) |
248 | e5fba493 | Klaus Aehlig | |
249 | b5fa2700 | Klaus Aehlig | handleCall qlock qstat cfg (SubmitManyJobs lops) = |
250 | 94d6d0a3 | Klaus Aehlig | do |
251 | 94d6d0a3 | Klaus Aehlig | open <- isQueueOpen |
252 | 94d6d0a3 | Klaus Aehlig | if not open |
253 | 94d6d0a3 | Klaus Aehlig | then return . Bad . GenericError $ "Queue drained" |
254 | 94d6d0a3 | Klaus Aehlig | else do |
255 | f5b765f0 | Klaus Aehlig | let mcs = Config.getMasterCandidates cfg |
256 | f5b765f0 | Klaus Aehlig | result_jobids <- allocateJobIds mcs qlock (length lops) |
257 | 94d6d0a3 | Klaus Aehlig | case result_jobids of |
258 | 94d6d0a3 | Klaus Aehlig | Bad s -> return . Bad . GenericError $ s |
259 | 94d6d0a3 | Klaus Aehlig | Ok jids -> do |
260 | c6013594 | Klaus Aehlig | ts <- currentTimestamp |
261 | c6013594 | Klaus Aehlig | jobs <- liftM (map $ setReceivedTimestamp ts) |
262 | c6013594 | Klaus Aehlig | $ zipWithM queuedJobFromOpCodes jids lops |
263 | 94d6d0a3 | Klaus Aehlig | qDir <- queueDir |
264 | 94d6d0a3 | Klaus Aehlig | write_results <- mapM (writeJobToDisk qDir) jobs |
265 | f5b765f0 | Klaus Aehlig | let annotated_results = zip write_results jobs |
266 | 94d6d0a3 | Klaus Aehlig | succeeded = map snd $ filter (isOk . fst) annotated_results |
267 | 94d6d0a3 | Klaus Aehlig | when (any isBad write_results) . logWarning |
268 | 94d6d0a3 | Klaus Aehlig | $ "Writing some jobs failed " ++ show annotated_results |
269 | f5b765f0 | Klaus Aehlig | replicateManyJobs qDir mcs succeeded |
270 | b5fa2700 | Klaus Aehlig | _ <- forkIO $ enqueueNewJobs qstat succeeded |
271 | 94d6d0a3 | Klaus Aehlig | return . Ok . JSArray |
272 | f5b765f0 | Klaus Aehlig | . map (\(res, job) -> |
273 | 94d6d0a3 | Klaus Aehlig | if isOk res |
274 | f5b765f0 | Klaus Aehlig | then showJSON (True, fromJobId $ qjId job) |
275 | 94d6d0a3 | Klaus Aehlig | else showJSON (False, genericResult id (const "") res)) |
276 | 94d6d0a3 | Klaus Aehlig | $ annotated_results |
277 | 229da00f | Petr Pudlak | |
278 | 6222b3a3 | Klaus Aehlig | handleCall _ _ cfg (WaitForJobChange jid fields prev_job prev_log tmout) = do |
279 | 9131274c | Jose A. Lopes | let compute_fn = computeJobUpdate cfg jid fields prev_log |
280 | 6222b3a3 | Klaus Aehlig | qDir <- queueDir |
281 | 6222b3a3 | Klaus Aehlig | -- verify if the job is finalized, and return immediately in this case |
282 | 6222b3a3 | Klaus Aehlig | jobresult <- loadJobFromDisk qDir False jid |
283 | 6222b3a3 | Klaus Aehlig | case jobresult of |
284 | 6222b3a3 | Klaus Aehlig | Ok (job, _) | not (jobFinalized job) -> do |
285 | 6222b3a3 | Klaus Aehlig | let jobfile = liveJobFile qDir jid |
286 | 6222b3a3 | Klaus Aehlig | answer <- watchFile jobfile (min tmout C.luxiWfjcTimeout) |
287 | 6222b3a3 | Klaus Aehlig | (prev_job, JSArray []) compute_fn |
288 | 6222b3a3 | Klaus Aehlig | return . Ok $ showJSON answer |
289 | 6222b3a3 | Klaus Aehlig | _ -> liftM (Ok . showJSON) compute_fn |
290 | 6222b3a3 | Klaus Aehlig | |
291 | 946f1fb3 | Klaus Aehlig | handleCall _ _ cfg (SetWatcherPause time) = do |
292 | 946f1fb3 | Klaus Aehlig | let mcs = Config.getMasterCandidates cfg |
293 | 946f1fb3 | Klaus Aehlig | masters = genericResult (const []) return |
294 | 946f1fb3 | Klaus Aehlig | . Config.getNode cfg . clusterMasterNode |
295 | 946f1fb3 | Klaus Aehlig | $ configCluster cfg |
296 | 946f1fb3 | Klaus Aehlig | _ <- executeRpcCall (masters ++ mcs) $ RpcCallSetWatcherPause time |
297 | 946f1fb3 | Klaus Aehlig | return . Ok . maybe JSNull showJSON $ time |
298 | 946f1fb3 | Klaus Aehlig | |
299 | d5665e10 | Klaus Aehlig | handleCall _ _ cfg (SetDrainFlag value) = do |
300 | d5665e10 | Klaus Aehlig | let mcs = Config.getMasterCandidates cfg |
301 | d5665e10 | Klaus Aehlig | fpath <- jobQueueDrainFile |
302 | d5665e10 | Klaus Aehlig | if value |
303 | d5665e10 | Klaus Aehlig | then writeFile fpath "" |
304 | d5665e10 | Klaus Aehlig | else removeFile fpath |
305 | d5665e10 | Klaus Aehlig | _ <- executeRpcCall mcs $ RpcCallSetDrainFlag value |
306 | d5665e10 | Klaus Aehlig | return . Ok . showJSON $ True |
307 | d5665e10 | Klaus Aehlig | |
308 | 36cb6837 | Klaus Aehlig | handleCall _ qstat cfg (CancelJob jid) = do |
309 | 36cb6837 | Klaus Aehlig | let jName = (++) "job " . show $ fromJobId jid |
310 | 36cb6837 | Klaus Aehlig | dequeueResult <- dequeueJob qstat jid |
311 | 36cb6837 | Klaus Aehlig | case dequeueResult of |
312 | 36cb6837 | Klaus Aehlig | Ok True -> do |
313 | 36cb6837 | Klaus Aehlig | logDebug $ jName ++ " dequeued, marking as canceled" |
314 | 36cb6837 | Klaus Aehlig | qDir <- queueDir |
315 | 36cb6837 | Klaus Aehlig | readResult <- loadJobFromDisk qDir True jid |
316 | 36cb6837 | Klaus Aehlig | let jobFileFailed = return . Ok . showJSON . (,) False |
317 | 36cb6837 | Klaus Aehlig | . (++) ("Dequeued " ++ jName |
318 | 36cb6837 | Klaus Aehlig | ++ ", but failed to mark as cancelled: ") |
319 | 36cb6837 | Klaus Aehlig | :: String -> IO (ErrorResult JSValue) |
320 | 36cb6837 | Klaus Aehlig | case readResult of |
321 | 36cb6837 | Klaus Aehlig | Bad s -> jobFileFailed s |
322 | 36cb6837 | Klaus Aehlig | Ok (job, _) -> do |
323 | 36cb6837 | Klaus Aehlig | now <- currentTimestamp |
324 | 36cb6837 | Klaus Aehlig | let job' = cancelQueuedJob now job |
325 | 36cb6837 | Klaus Aehlig | mcs = Config.getMasterCandidates cfg |
326 | 36cb6837 | Klaus Aehlig | write_result <- writeJobToDisk qDir job' |
327 | 36cb6837 | Klaus Aehlig | case write_result of |
328 | 36cb6837 | Klaus Aehlig | Bad s -> jobFileFailed s |
329 | 36cb6837 | Klaus Aehlig | Ok () -> do |
330 | 36cb6837 | Klaus Aehlig | replicateManyJobs qDir mcs [job'] |
331 | 36cb6837 | Klaus Aehlig | return . Ok . showJSON $ (True, "Dequeued " ++ jName) |
332 | 36cb6837 | Klaus Aehlig | Ok False -> do |
333 | 36cb6837 | Klaus Aehlig | logDebug $ jName ++ " not queued; trying to cancel directly" |
334 | 36cb6837 | Klaus Aehlig | cancelJob jid |
335 | 36cb6837 | Klaus Aehlig | Bad s -> return . Ok . showJSON $ (False, s) |
336 | 36cb6837 | Klaus Aehlig | |
337 | b5fa2700 | Klaus Aehlig | handleCall _ _ _ op = |
338 | 5183e8be | Iustin Pop | return . Bad $ |
339 | 5183e8be | Iustin Pop | GenericError ("Luxi call '" ++ strOfOp op ++ "' not implemented") |
340 | 25b54de0 | Iustin Pop | |
341 | edcad688 | Petr Pudlak | {-# ANN handleCall "HLint: ignore Too strict if" #-} |
342 | edcad688 | Petr Pudlak | |
343 | 6222b3a3 | Klaus Aehlig | -- | Query the status of a job and return the requested fields |
344 | 6222b3a3 | Klaus Aehlig | -- and the logs newer than the given log number. |
345 | 9131274c | Jose A. Lopes | computeJobUpdate :: ConfigData -> JobId -> [String] -> JSValue |
346 | 6222b3a3 | Klaus Aehlig | -> IO (JSValue, JSValue) |
347 | 6222b3a3 | Klaus Aehlig | computeJobUpdate cfg jid fields prev_log = do |
348 | 6222b3a3 | Klaus Aehlig | let sjid = show $ fromJobId jid |
349 | 6222b3a3 | Klaus Aehlig | logDebug $ "Inspecting fields " ++ show fields ++ " of job " ++ sjid |
350 | 6222b3a3 | Klaus Aehlig | let fromJSArray (JSArray xs) = xs |
351 | 6222b3a3 | Klaus Aehlig | fromJSArray _ = [] |
352 | 6222b3a3 | Klaus Aehlig | let logFilter JSNull (JSArray _) = True |
353 | 6222b3a3 | Klaus Aehlig | logFilter (JSRational _ n) (JSArray (JSRational _ m:_)) = n < m |
354 | 6222b3a3 | Klaus Aehlig | logFilter _ _ = False |
355 | 6222b3a3 | Klaus Aehlig | let filterLogs n logs = JSArray (filter (logFilter n) (logs >>= fromJSArray)) |
356 | 6222b3a3 | Klaus Aehlig | jobQuery <- handleClassicQuery cfg (Qlang.ItemTypeLuxi Qlang.QRJob) |
357 | 6222b3a3 | Klaus Aehlig | [Right . fromIntegral $ fromJobId jid] ("oplog" : fields) False |
358 | 6222b3a3 | Klaus Aehlig | let (rfields, rlogs) = case jobQuery of |
359 | 6222b3a3 | Klaus Aehlig | Ok (JSArray [JSArray (JSArray logs : answer)]) -> |
360 | 6222b3a3 | Klaus Aehlig | (answer, filterLogs prev_log logs) |
361 | 6222b3a3 | Klaus Aehlig | _ -> (map (const JSNull) fields, JSArray []) |
362 | 6222b3a3 | Klaus Aehlig | logDebug $ "Updates for job " ++ sjid ++ " are " ++ encode (rfields, rlogs) |
363 | 6222b3a3 | Klaus Aehlig | return (JSArray rfields, rlogs) |
364 | 6222b3a3 | Klaus Aehlig | |
365 | d79a6502 | Petr Pudlak | |
366 | d79a6502 | Petr Pudlak | type LuxiConfig = (MVar (), JQStatus, ConfigReader) |
367 | d79a6502 | Petr Pudlak | |
368 | d79a6502 | Petr Pudlak | luxiExec |
369 | d79a6502 | Petr Pudlak | :: LuxiConfig |
370 | d79a6502 | Petr Pudlak | -> LuxiOp |
371 | d79a6502 | Petr Pudlak | -> IO (Bool, GenericResult GanetiException JSValue) |
372 | d79a6502 | Petr Pudlak | luxiExec (qlock, qstat, creader) args = do |
373 | 25b54de0 | Iustin Pop | cfg <- creader |
374 | d79a6502 | Petr Pudlak | result <- handleCallWrapper qlock qstat cfg args |
375 | d79a6502 | Petr Pudlak | return (True, result) |
376 | d79a6502 | Petr Pudlak | |
377 | d79a6502 | Petr Pudlak | luxiHandler :: LuxiConfig -> U.Handler LuxiOp JSValue |
378 | d79a6502 | Petr Pudlak | luxiHandler cfg = U.Handler { U.hParse = decodeLuxiCall |
379 | d79a6502 | Petr Pudlak | , U.hInputLogShort = strOfOp |
380 | d79a6502 | Petr Pudlak | , U.hInputLogLong = show |
381 | d79a6502 | Petr Pudlak | , U.hExec = luxiExec cfg |
382 | d79a6502 | Petr Pudlak | } |
383 | d79a6502 | Petr Pudlak | |
384 | 670e954a | Thomas Thrainer | -- | Type alias for prepMain results |
385 | 5e671e0e | Petr Pudlak | type PrepResult = (Server, IORef (Result ConfigData), JQStatus) |
386 | 25b54de0 | Iustin Pop | |
387 | 3695a4e0 | Thomas Thrainer | -- | Check function for luxid. |
388 | 670e954a | Thomas Thrainer | checkMain :: CheckFn () |
389 | 670e954a | Thomas Thrainer | checkMain _ = return $ Right () |
390 | 670e954a | Thomas Thrainer | |
391 | 3695a4e0 | Thomas Thrainer | -- | Prepare function for luxid. |
392 | 670e954a | Thomas Thrainer | prepMain :: PrepFn () PrepResult |
393 | 670e954a | Thomas Thrainer | prepMain _ _ = do |
394 | 670e954a | Thomas Thrainer | socket_path <- Path.defaultQuerySocket |
395 | 0d0ac025 | Iustin Pop | cleanupSocket socket_path |
396 | 73b16ca1 | Iustin Pop | s <- describeError "binding to the Luxi socket" |
397 | d605e261 | Petr Pudlak | Nothing (Just socket_path) $ getLuxiServer True socket_path |
398 | 670e954a | Thomas Thrainer | cref <- newIORef (Bad "Configuration not yet loaded") |
399 | 9131274c | Jose A. Lopes | jq <- emptyJQStatus |
400 | 5e671e0e | Petr Pudlak | return (s, cref, jq) |
401 | 670e954a | Thomas Thrainer | |
402 | 670e954a | Thomas Thrainer | -- | Main function. |
403 | 670e954a | Thomas Thrainer | main :: MainFn () PrepResult |
404 | 5e671e0e | Petr Pudlak | main _ _ (server, cref, jq) = do |
405 | 670e954a | Thomas Thrainer | initConfigReader id cref |
406 | 670e954a | Thomas Thrainer | let creader = readIORef cref |
407 | b5fa2700 | Klaus Aehlig | initJQScheduler jq |
408 | 9131274c | Jose A. Lopes | |
409 | e5fba493 | Klaus Aehlig | qlockFile <- jobQueueLockFile |
410 | e5fba493 | Klaus Aehlig | lockFile qlockFile >>= exitIfBad "Failed to obtain the job-queue lock" |
411 | e5fba493 | Klaus Aehlig | qlock <- newMVar () |
412 | 4c3f55b8 | Iustin Pop | |
413 | 4c3f55b8 | Iustin Pop | finally |
414 | c7003a76 | Petr Pudlak | (forever $ U.listener (luxiHandler (qlock, jq, creader)) server) |
415 | 5e671e0e | Petr Pudlak | (closeServer server) |