Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.warden; |
| 18 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 22 | import com.google.common.io.ByteStreams; |
| 23 | |
| 24 | import java.io.File; |
| 25 | import java.io.FileInputStream; |
| 26 | import java.io.FileOutputStream; |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.io.PrintWriter; |
| 30 | import java.text.SimpleDateFormat; |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 31 | import java.util.ArrayList; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 32 | import java.util.Date; |
| 33 | import java.util.HashSet; |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 34 | import java.util.List; |
| 35 | import java.util.Map; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 36 | import java.util.Random; |
| 37 | import java.util.Set; |
| 38 | import java.util.Timer; |
| 39 | import java.util.TimerTask; |
| 40 | import java.util.concurrent.TimeUnit; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 41 | |
| 42 | import static com.google.common.base.Preconditions.*; |
| 43 | |
| 44 | /** |
| 45 | * Warden for tracking use of shared test cells. |
| 46 | */ |
| 47 | class Warden { |
| 48 | |
| 49 | private static final String CELL_NOT_NULL = "Cell name cannot be null"; |
| 50 | private static final String USER_NOT_NULL = "User name cannot be null"; |
| 51 | private static final String KEY_NOT_NULL = "User key cannot be null"; |
| 52 | private static final String UTF_8 = "UTF-8"; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | 0d33700 | 2016-05-04 10:00:18 -0700 | [diff] [blame] | 54 | private static final long TIMEOUT = 10; // 10 seconds |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 55 | private static final int MAX_MINUTES = 240; // 4 hours max |
| 56 | private static final int MINUTE = 60_000; // 1 minute |
Thomas Vachuska | f9872a0 | 2016-05-05 13:56:25 -0700 | [diff] [blame] | 57 | private static final int DEFAULT_MINUTES = 60; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 58 | |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 59 | private static final String DEFAULT_SPEC = "3+1"; |
| 60 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 61 | private final File log = new File("warden.log"); |
| 62 | |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 63 | // Allow overriding these for unit tests. |
| 64 | static String cmdPrefix = ""; |
| 65 | static File root = new File("."); |
| 66 | |
| 67 | private final File cells = new File(root, "cells"); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 68 | private final File supported = new File(cells, "supported"); |
| 69 | private final File reserved = new File(cells, "reserved"); |
| 70 | |
| 71 | private final Random random = new Random(); |
| 72 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Creates a new cell warden. |
| 75 | */ |
| 76 | Warden() { |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 77 | reserved.mkdirs(); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 78 | random.setSeed(System.currentTimeMillis()); |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 79 | Timer timer = new Timer("cell-pruner", true); |
Thomas Vachuska | 71ff332 | 2016-05-03 15:33:05 -0700 | [diff] [blame] | 80 | timer.schedule(new Reposessor(), MINUTE / 4, MINUTE / 2); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns list of names of supported cells. |
| 85 | * |
| 86 | * @return list of cell names |
| 87 | */ |
| 88 | Set<String> getCells() { |
| 89 | String[] list = supported.list(); |
| 90 | return list != null ? ImmutableSet.copyOf(list) : ImmutableSet.of(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns list of names of available cells. |
| 95 | * |
| 96 | * @return list of cell names |
| 97 | */ |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 98 | Set<String> getAvailableCells() { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 99 | Set<String> available = new HashSet<>(getCells()); |
| 100 | available.removeAll(getReservedCells()); |
| 101 | return ImmutableSet.copyOf(available); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns list of names of reserved cells. |
| 106 | * |
| 107 | * @return list of cell names |
| 108 | */ |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 109 | Set<String> getReservedCells() { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 110 | String[] list = reserved.list(); |
| 111 | return list != null ? ImmutableSet.copyOf(list) : ImmutableSet.of(); |
| 112 | } |
| 113 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 114 | /** |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 115 | * Returns the host name on which the specified cell is hosted. |
| 116 | * |
| 117 | * @param cellName cell name |
| 118 | * @return host name where the cell runs |
| 119 | */ |
| 120 | String getCellHost(String cellName) { |
| 121 | return getCellInfo(cellName).hostName; |
| 122 | } |
| 123 | |
| 124 | /** |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 125 | * Returns reservation for the specified user. |
| 126 | * |
| 127 | * @param userName user name |
| 128 | * @return cell reservation record or null if user does not have one |
| 129 | */ |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 130 | Reservation currentUserReservation(String userName) { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 131 | checkNotNull(userName, USER_NOT_NULL); |
| 132 | for (String cellName : getReservedCells()) { |
| 133 | Reservation reservation = currentCellReservation(cellName); |
| 134 | if (reservation != null && userName.equals(reservation.userName)) { |
| 135 | return reservation; |
| 136 | } |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the name of the user who reserved the given cell. |
| 143 | * |
| 144 | * @param cellName cell name |
| 145 | * @return cell reservation record or null if cell is not reserved |
| 146 | */ |
| 147 | Reservation currentCellReservation(String cellName) { |
| 148 | checkNotNull(cellName, CELL_NOT_NULL); |
| 149 | File cellFile = new File(reserved, cellName); |
| 150 | if (!cellFile.exists()) { |
| 151 | return null; |
| 152 | } |
| 153 | try (InputStream stream = new FileInputStream(cellFile)) { |
| 154 | return new Reservation(new String(ByteStreams.toByteArray(stream), "UTF-8")); |
| 155 | } catch (IOException e) { |
| 156 | throw new IllegalStateException("Unable to get current user for cell " + cellName, e); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Reserves a cell for the specified user and their public access key. |
| 162 | * |
| 163 | * @param userName user name |
| 164 | * @param sshKey user ssh public key |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 165 | * @param minutes optional number of minutes for reservation |
| 166 | * @param cellSpec optional cell specification string |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 167 | * @return reserved cell definition |
| 168 | */ |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 169 | synchronized String borrowCell(String userName, String sshKey, int minutes, |
| 170 | String cellSpec) { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 171 | checkNotNull(userName, USER_NOT_NULL); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 172 | checkArgument(userName.matches("[\\w]+"), "Invalid user name %s", userName); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 173 | checkNotNull(sshKey, KEY_NOT_NULL); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 174 | checkArgument(minutes < MAX_MINUTES, "Number of minutes must be less than %d", MAX_MINUTES); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 175 | checkArgument(minutes >= 0, "Number of minutes must be non-negative"); |
| 176 | checkArgument(cellSpec == null || cellSpec.matches("[\\d]+\\+[0-1]"), |
| 177 | "Invalid cell spec string %s", cellSpec); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 178 | Reservation reservation = currentUserReservation(userName); |
| 179 | if (reservation == null) { |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 180 | // If there is no reservation for the user, create one |
| 181 | String cellName = findAvailableCell(); |
| 182 | reservation = new Reservation(cellName, userName, System.currentTimeMillis(), |
| 183 | minutes == 0 ? DEFAULT_MINUTES : minutes, |
| 184 | cellSpec == null ? DEFAULT_SPEC : cellSpec); |
Thomas Vachuska | 0d33700 | 2016-05-04 10:00:18 -0700 | [diff] [blame] | 185 | } else if (minutes == 0) { |
| 186 | // If minutes are 0, simply return the cell definition |
| 187 | return getCellDefinition(reservation.cellName); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 188 | } else { |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 189 | // If minutes are > 0, update the existing cell reservation |
| 190 | reservation = new Reservation(reservation.cellName, userName, |
| 191 | System.currentTimeMillis(), minutes, |
| 192 | reservation.cellSpec); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 195 | reserveCell(reservation); |
| 196 | createCell(reservation, sshKey); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 197 | log(userName, reservation.cellName, reservation.cellSpec, |
| 198 | "borrowed for " + reservation.duration + " minutes"); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 199 | return getCellDefinition(reservation.cellName); |
| 200 | } |
| 201 | |
| 202 | /** |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 203 | * Returns name of an available cell. Cell is chosen based on the load |
| 204 | * of its hosting server; a random one will be chosen from the set of |
| 205 | * cells hosted by the least loaded server. |
| 206 | * |
| 207 | * @return name of an available cell |
| 208 | */ |
| 209 | private String findAvailableCell() { |
| 210 | Set<String> cells = getAvailableCells(); |
| 211 | checkState(!cells.isEmpty(), "No cells are presently available"); |
| 212 | Map<String, ServerInfo> load = Maps.newHashMap(); |
| 213 | |
| 214 | cells.stream().map(this::getCellInfo) |
| 215 | .forEach(info -> load.compute(info.hostName, (k, v) -> v == null ? |
| 216 | new ServerInfo(info.hostName) : v.bumpLoad(info))); |
| 217 | |
| 218 | List<ServerInfo> servers = new ArrayList<>(load.values()); |
Thomas Vachuska | ea31609 | 2016-05-16 09:50:44 -0700 | [diff] [blame^] | 219 | servers.sort((a, b) -> b.load - a.load); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 220 | ServerInfo server = servers.get(0); |
| 221 | return server.cells.get(random.nextInt(server.cells.size())).cellName; |
| 222 | } |
| 223 | |
| 224 | /** |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 225 | * Returns the specified cell for the specified user and their public access key. |
| 226 | * |
| 227 | * @param userName user name |
| 228 | */ |
| 229 | synchronized void returnCell(String userName) { |
| 230 | checkNotNull(userName, USER_NOT_NULL); |
| 231 | Reservation reservation = currentUserReservation(userName); |
| 232 | checkState(reservation != null, "User %s has no cell reservations", userName); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 233 | |
| 234 | unreserveCell(reservation); |
| 235 | destroyCell(reservation); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 236 | log(userName, reservation.cellName, reservation.cellSpec, "returned"); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Reserves the specified cell for the user the source file and writes the |
| 241 | * specified content to the target file. |
| 242 | * |
| 243 | * @param reservation cell reservation record |
| 244 | */ |
| 245 | private void reserveCell(Reservation reservation) { |
| 246 | File cellFile = new File(reserved, reservation.cellName); |
| 247 | try (FileOutputStream stream = new FileOutputStream(cellFile)) { |
| 248 | stream.write(reservation.encode().getBytes(UTF_8)); |
| 249 | } catch (IOException e) { |
| 250 | throw new IllegalStateException("Unable to reserve cell " + reservation.cellName, e); |
| 251 | } |
| 252 | } |
| 253 | |
Thomas Vachuska | 5617994 | 2016-05-10 21:15:49 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Returns the cell definition of the specified cell. |
| 256 | * |
| 257 | * @param cellName cell name |
| 258 | * @return cell definition |
| 259 | */ |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 260 | private String getCellDefinition(String cellName) { |
Thomas Vachuska | 5617994 | 2016-05-10 21:15:49 -0700 | [diff] [blame] | 261 | CellInfo cellInfo = getCellInfo(cellName); |
| 262 | return exec(String.format("ssh %s warden/bin/cell-def %s", |
| 263 | cellInfo.hostName, cellInfo.cellName)); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Cancels the specified reservation. |
| 268 | * |
| 269 | * @param reservation reservation record |
| 270 | */ |
| 271 | private void unreserveCell(Reservation reservation) { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 272 | checkState(new File(reserved, reservation.cellName).delete(), |
| 273 | "Unable to return cell %s", reservation.cellName); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Creates the cell for the specified user SSH key. |
| 278 | * |
| 279 | * @param reservation cell reservation |
| 280 | * @param sshKey ssh key |
| 281 | */ |
| 282 | private void createCell(Reservation reservation, String sshKey) { |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 283 | CellInfo cellInfo = getCellInfo(reservation.cellName); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 284 | String cmd = String.format("ssh %s warden/bin/create-cell %s %s %s %s", |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 285 | cellInfo.hostName, cellInfo.cellName, |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 286 | cellInfo.ipPrefix, reservation.cellSpec, sshKey); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 287 | exec(cmd); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Destroys the specified cell. |
| 292 | * |
| 293 | * @param reservation reservation record |
| 294 | */ |
| 295 | private void destroyCell(Reservation reservation) { |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 296 | CellInfo cellInfo = getCellInfo(reservation.cellName); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 297 | exec(String.format("ssh %s warden/bin/destroy-cell %s %s", |
| 298 | cellInfo.hostName, cellInfo.cellName, reservation.cellSpec)); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 302 | * Reads the information about the specified cell. |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 303 | * |
| 304 | * @param cellName cell name |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 305 | * @return cell information |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 306 | */ |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 307 | private CellInfo getCellInfo(String cellName) { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 308 | File cellFile = new File(supported, cellName); |
| 309 | try (InputStream stream = new FileInputStream(cellFile)) { |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 310 | String[] fields = new String(ByteStreams.toByteArray(stream), UTF_8).split(" "); |
| 311 | return new CellInfo(cellName, fields[0], fields[1]); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 312 | } catch (IOException e) { |
| 313 | throw new IllegalStateException("Unable to definition for cell " + cellName, e); |
| 314 | } |
| 315 | } |
| 316 | |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 317 | // Executes the specified command. |
| 318 | private String exec(String command) { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 319 | try { |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 320 | Process process = Runtime.getRuntime().exec(cmdPrefix + command); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 321 | String output = new String(ByteStreams.toByteArray(process.getInputStream()), UTF_8); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 322 | process.waitFor(TIMEOUT, TimeUnit.SECONDS); |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 323 | return process.exitValue() == 0 ? output : null; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 324 | } catch (Exception e) { |
Thomas Vachuska | e91541f | 2016-05-05 23:15:41 -0700 | [diff] [blame] | 325 | throw new IllegalStateException("Unable to execute " + command); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | // Creates an audit log entry. |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 330 | private void log(String userName, String cellName, String cellSpec, String action) { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 331 | try (FileOutputStream fos = new FileOutputStream(log, true); |
| 332 | PrintWriter pw = new PrintWriter(fos)) { |
| 333 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 334 | pw.println(String.format("%s\t%s\t%s-%s\t%s", format.format(new Date()), |
| 335 | userName, cellName, cellSpec, action)); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 336 | pw.flush(); |
| 337 | } catch (IOException e) { |
| 338 | throw new IllegalStateException("Unable to log reservation action", e); |
| 339 | } |
| 340 | } |
| 341 | |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 342 | // Carrier of cell information |
| 343 | private final class CellInfo { |
| 344 | final String cellName; |
| 345 | final String hostName; |
| 346 | final String ipPrefix; |
| 347 | |
| 348 | private CellInfo(String cellName, String hostName, String ipPrefix) { |
| 349 | this.cellName = cellName; |
| 350 | this.hostName = hostName; |
| 351 | this.ipPrefix = ipPrefix; |
| 352 | } |
| 353 | } |
| 354 | |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 355 | // Carrier of cell server information |
| 356 | private final class ServerInfo { |
| 357 | final String hostName; |
| 358 | int load = 0; |
| 359 | List<CellInfo> cells = Lists.newArrayList(); |
| 360 | |
| 361 | private ServerInfo(String hostName) { |
| 362 | this.hostName = hostName; |
| 363 | } |
| 364 | |
| 365 | private ServerInfo bumpLoad(CellInfo info) { |
| 366 | cells.add(info); |
| 367 | load++; // TODO: bump by cell size later |
| 368 | return this; |
| 369 | } |
| 370 | } |
| 371 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 372 | // Task for re-possessing overdue cells |
Thomas Vachuska | 419c8bd | 2016-05-10 14:36:07 -0700 | [diff] [blame] | 373 | private final class Reposessor extends TimerTask { |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 374 | @Override |
| 375 | public void run() { |
| 376 | long now = System.currentTimeMillis(); |
| 377 | for (String cellName : getReservedCells()) { |
| 378 | Reservation reservation = currentCellReservation(cellName); |
| 379 | if (reservation != null && |
| 380 | (reservation.time + reservation.duration * MINUTE) < now) { |
| 381 | try { |
| 382 | returnCell(reservation.userName); |
| 383 | } catch (Exception e) { |
| 384 | e.printStackTrace(); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |