1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
| package graph
import java.io.FileInputStream import java.time.{Instant, LocalDateTime, ZoneOffset} import java.time.format.DateTimeFormatter import java.util.Properties
import scala.collection.mutable.ListBuffer
object Graph {
case class Node(name: String, flag: String, cost: Long, path: String)
case class Edge(from: String, to: String)
var mysqlData: MySQLData = null
var nodeSet: Set[Node] = Set[Node]() var edgeSet: Set[Edge] = Set[Edge]() var eSet = Set[Edge]() var nSet = Set[Node]() var recoverSet = Set[Node]() var pickedHeadSet = Set[Node]()
var nodeMap: Map[String, Node] = Map[String, Node]()
var THREAD_COUNT = 3 val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val LOCK = 1l var recover = true
def init(): Unit = { nodeSet += Node("a", "t", 0, "执行命令") nodeSet += Node("b", "t", 0, "执行命令") nodeSet += Node("c", "t", 0, "执行命令") nodeSet += Node("d", "p", 7, "执行命令d") nodeSet += Node("x", "t", 0, "执行命令") nodeSet += Node("e", "t", 0, "执行命令") nodeSet += Node("f", "p", 6, "执行命令f") nodeSet += Node("g", "t", 0, "执行命令") nodeSet += Node("h", "t", 0, "执行命令") nodeSet += Node("i", "t", 0, "执行命令") nodeSet += Node("j", "p", 5, "执行命令j") nodeSet += Node("k", "p", 4, "执行命令k")
edgeSet += Edge("a", "d") edgeSet += Edge("b", "d") edgeSet += Edge("c", "d") edgeSet += Edge("d", "e") edgeSet += Edge("e", "f") edgeSet += Edge("f", "g") edgeSet += Edge("f", "h") edgeSet += Edge("g", "j") edgeSet += Edge("h", "j") edgeSet += Edge("h", "k") edgeSet += Edge("i", "j") edgeSet += Edge("j", "i")
}
def generate(): Unit = { nodeMap = nodeSet.map(x => (x.name, x)).toMap[String, Node]
val edge1 = edgeSet.filter(x => nodeMap.filter(_._2.flag == "p").keys.toList.contains(x.from)).map(x => (x.from, x.to)).toList
val p2p = edge1.filter(x => { nodeMap(x._1).flag.equals("p") && nodeMap(x._2).flag.equals("p") }) val edge2 = edgeSet.filter(x => edge1.map(_._2).contains(x.from)).map(x => (x.from, x.to)).toList
nodeSet = nodeSet.filter(_.flag == "p") nodeMap = nodeSet.map(x => (x.name, x)).toMap[String, Node] edgeSet = edge1.map(x => (x, edge2.filter(_._1 == x._2).map(_._2))).map(x => { x._2.map(y => { (x._1._1, y) })
}).flatMap(_.toList).++(p2p).map(x => { Edge(x._1, x._2) }).toSet println(edge1) println(edge2) println(nodeSet) println(edgeSet) }
def validate(): Unit = { edgeSet = edgeSet.filter(x => nodeMap.get(x.from).isDefined && nodeMap.get(x.to).isDefined) eSet = edgeSet nSet = nodeSet }
def getReset(rSet: Set[Node]): Set[Node] = { val set = if (rSet.size < 1) Set[Node]() else getReset(edgeSet.filter(x => { rSet.map(_.name).contains(x.from) }).map(x => nodeMap(x.to))) set ++ rSet }
def main(args: Array[String]): Unit = { val prop = new Properties() prop.load(new FileInputStream("D:\\Projects\\IdeaProjects\\Demo\\zsd-test\\src\\main\\resource\\prop.properties")) THREAD_COUNT = prop.getProperty("worker_count").toInt if (args.length > 1) { recover = true }
init() generate()
for (_ <- 0 until THREAD_COUNT) { val thread = new Worker() thread.start() } }
class Worker extends Thread { val runtime: Runtime = Runtime.getRuntime var history: ListBuffer[Node] = ListBuffer[Node]() var cost = 0d var enable = true
override def run(): Unit = { while (nSet.nonEmpty && enable) { var pickedNode: Node = null LOCK.synchronized { val headSet = getHead println(headSet) if (headSet.nonEmpty) { val (pn, length) = pickNode(headSet) history += pn pickedHeadSet += pn eSet = eSet.filter(_.from != pn.name) nSet = nSet.filter(_ != pn) pickedNode = pn } else { println(s"Thread ${Thread.currentThread().getId} Sleeping !!") Thread.sleep(10000) } }
if (pickedNode != null) { println(s"Thread ${Thread.currentThread().getId} ==> ${pickedNode.name}") val t1 = System.currentTimeMillis() var t2 = t1 var rtn = 0 try { println(s"Exec ${pickedNode.path}") Thread.sleep(pickedNode.cost * 1000)
println(pickedNode.path) t2 = System.currentTimeMillis() if (rtn != 0) { throw new RuntimeException(s"Return Code $rtn !!") } else { println(s"Run Success in Code $rtn !! Cost ${(t2 - t1) / 1000} Seconds") }
LOCK.synchronized { pickedHeadSet -= pickedNode } } catch { case e: RuntimeException => { LOCK.synchronized { println(s"### Node ${pickedNode.name} # is error !!") printChildren(pickedNode, pickedNode) println(e) } } } finally { val time = formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(t1), ZoneOffset.ofHours(8))) val sql = new StringBuilder(s"update job_node set last_run='$time'") if (rtn == 0) { cost += (t2 - t1) / 1000 sql.append(s", cost = ${(t2 - t1) / 1000}") } sql.append(s" where name='${pickedNode.name}'") } } } println(s"@@ Thread ${Thread.currentThread().getId} Cost: $cost ==> $history") }
def printChildren(node: Node, error_root: Node): Unit = { println(s"Record the Error Node ${node.name}!!") nSet -= node edgeSet.filter(_.from == node.name).foreach(x => { printChildren(nodeMap(x.to), error_root) }) }
def getDepth(node: Node, nodeSeq: Seq[Node], depth: Double = 0): (Seq[Node], Double) = { val subEdgeSet = edgeSet.filter(_.from == node.name) if (subEdgeSet.isEmpty) { (nodeSeq :+ node, node.cost + depth) } else subEdgeSet.map(x => { getDepth(nodeMap(x.to), nodeSeq :+ node, node.cost + depth) }).maxBy(_._2) }
def pickNode(headSet: Set[Node]): (Node, Double) = { val maxNode = headSet.map(node => (node, getDepth(node, Seq[Node]()))).maxBy(_._2._2) (maxNode._1, maxNode._2._2) }
def getHead: Set[Node] = { nSet.filterNot(node => { eSet.map(_.to).contains(node.name) || pickedHeadSet.map(_.name).contains(node.name) || edgeSet.filter(x => pickedHeadSet.map(_.name).contains(x.from)).map(_.to).contains(node.name) }) } } }
---
package graph
import java.sql.{Connection, DriverManager, ResultSet}
class MySQLData(url: String, username: String, password: String) { val lock = 1l var conn: Connection = null
def initConn(): Unit = { lock.synchronized { if (conn == null) { Class.forName("com.mysql.jdbc.Driver") conn = DriverManager.getConnection(url, username, password) } } }
def executeQuery(sql: String): ResultSet = { initConn()
val statement = conn.createStatement() statement.executeQuery(sql) }
def executeUpdate(sql: String): Int = { initConn()
val statement = conn.createStatement() statement.executeUpdate(sql) } }
object MySQLData { def apply( url: String, username: String, password: String ): MySQLData = new MySQLData(url, username, password) }
|