├── Jenkinsfile ├── LICENSE ├── README.md ├── durable-memory.png ├── ignitedurablememory.jpg ├── mapReduce.png ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── romeh │ │ └── ignitemanager │ │ ├── AlertManagerApplication.java │ │ ├── SwaggerConfiguration.java │ │ ├── compute │ │ ├── DataGridCompute.java │ │ ├── FailFastReducer.java │ │ ├── MapReduceResponse.java │ │ └── ServiceResponse.java │ │ ├── config │ │ └── AlertManagerConfiguration.java │ │ ├── entities │ │ ├── AlertConfigEntry.java │ │ ├── AlertEntry.java │ │ ├── AlertHolder.java │ │ ├── AlertsConfiguration.java │ │ ├── CacheNames.java │ │ └── CustomErrorResponse.java │ │ ├── exception │ │ └── ResourceNotFoundException.java │ │ ├── repositories │ │ ├── AlertsConfigStore.java │ │ ├── AlertsStore.java │ │ └── impl │ │ │ ├── IgniteAlertConfigStore.java │ │ │ └── IgniteAlertsStore.java │ │ ├── services │ │ ├── AlertsService.java │ │ ├── CleanExpiredAlertsService.java │ │ ├── ComputeService.java │ │ ├── DataLoaderService.java │ │ └── MailService.java │ │ └── web │ │ ├── AlertsController.java │ │ └── ErrorHandler.java └── resources │ ├── application.yml │ ├── logback.xml │ └── templates │ └── ticket.html └── test ├── java └── com │ └── romeh │ └── ignitemanager │ ├── integration │ └── AlertManagerApplicationIT.java │ ├── repositories │ └── impl │ │ └── IgniteAlertsSoreTest.java │ └── web │ └── AlertsControllerTest.java └── resources ├── application.yml ├── ignite.xml └── logback-test.xml /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | // run on jenkins nodes tha has java 8 3 | agent { label 'jdk8' } 4 | // global env variables 5 | environment { 6 | EMAIL_RECIPIENTS = 'mahmoud.romih@test.com' 7 | } 8 | stages { 9 | stage('Build With Unit Testing') { 10 | steps { 11 | // Run the maven build 12 | script { 13 | // Get the Maven tool. 14 | // ** NOTE: This 'M3' Maven tool must be configured 15 | // ** in the global configuration. 16 | echo 'Pulling...' + env.BRANCH_NAME 17 | def mvnHome = tool 'Maven 3.3.9' 18 | if (isUnix()) { 19 | sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package" 20 | def pom = readMavenPom file: 'pom.xml' 21 | print pom.version 22 | junit '**/target/surefire-reports/TEST-*.xml' 23 | archive 'target/*.jar' 24 | } else { 25 | bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/) 26 | def pom = readMavenPom file: 'pom.xml' 27 | print pom.version 28 | junit '**/target/surefire-reports/TEST-*.xml' 29 | archive 'target/*.jar' 30 | } 31 | } 32 | 33 | } 34 | } 35 | stage('Integration Tests') { 36 | // Run the maven build 37 | steps { 38 | script { 39 | def mvnHome = tool 'Maven 3.3.9' 40 | if (isUnix()) { 41 | sh "'${mvnHome}/bin/mvn' verify -Dunit-tests.skip=true" 42 | } else { 43 | bat(/"${mvnHome}\bin\mvn" verify -Dunit-tests.skip=true/) 44 | } 45 | 46 | } 47 | } 48 | } 49 | stage('Sonar Check') { 50 | // Run the maven build 51 | steps { 52 | script { 53 | def mvnHome = tool 'Maven 3.3.9' 54 | // replace it with your sonar server 55 | sh "'${mvnHome}/bin/mvn' verify sonar:sonar -Dsonar.host.url=http://romehjava.bc/sonar/ -Dmaven.test.failure.ignore=true" 56 | 57 | } 58 | } 59 | } 60 | 61 | stage('ITT Deploy Approval and deployment') { 62 | when { 63 | // check if the build was successful 64 | expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' && env.BRANCH_NAME == 'master' } 65 | } 66 | steps { 67 | timeout(time: 3, unit: 'MINUTES') { 68 | //input message:'Approve deployment?', submitter: 'it-ops' 69 | input message: 'Approve deployment?' 70 | } 71 | timeout(time: 2, unit: 'MINUTES') { 72 | // call another jenkins job which do ssh deployment 73 | build job: 'AlertManagerToITT' 74 | echo 'the application is deployed !' 75 | } 76 | } 77 | } 78 | } 79 | post { 80 | // Always runs. And it runs before any of the other post conditions. 81 | always { 82 | // Let's wipe out the workspace before we finish! 83 | deleteDir() 84 | } 85 | success { 86 | sendEmail("Successful"); 87 | } 88 | unstable { 89 | sendEmail("Unstable"); 90 | } 91 | failure { 92 | sendEmail("Failed"); 93 | } 94 | } 95 | 96 | // The options directive is for configuration that applies to the whole job. 97 | options { 98 | // For example, we'd like to make sure we only keep 10 builds at a time, so 99 | // we don't fill up our storage! 100 | buildDiscarder(logRotator(numToKeepStr: '10')) 101 | 102 | // And we'd really like to be sure that this build doesn't hang forever, so 103 | // let's time it out after an hour. 104 | timeout(time: 20, unit: 'MINUTES') 105 | } 106 | 107 | } 108 | 109 | 110 | @NonCPS 111 | def getChangeString() { 112 | MAX_MSG_LEN = 100 113 | def changeString = "" 114 | 115 | echo "Gathering SCM changes" 116 | def changeLogSets = currentBuild.changeSets 117 | for (int i = 0; i < changeLogSets.size(); i++) { 118 | def entries = changeLogSets[i].items 119 | for (int j = 0; j < entries.length; j++) { 120 | def entry = entries[j] 121 | truncated_msg = entry.msg.take(MAX_MSG_LEN) 122 | changeString += " - ${truncated_msg} [${entry.author}]\n" 123 | } 124 | } 125 | 126 | if (!changeString) { 127 | changeString = " - No new changes" 128 | } 129 | return changeString 130 | } 131 | 132 | def sendEmail(status) { 133 | mail( 134 | to: "$EMAIL_RECIPIENTS", 135 | subject: "Build $BUILD_NUMBER - " + status + " (${currentBuild.fullDisplayName})", 136 | body: "Changes:\n " + getChangeString() + "\n\n Check console output at: $BUILD_URL/console" + "\n") 137 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot integration with Apache Ignite and its durable memory and sql queries over cache 2 | 3 | show case for how integrate apache ignite with spring boot plus using the durable memory feature and sql queries over ignite in memory caches 4 | 5 | * Integrate spring boot with Apache Ignite 6 | * How to enable and use persistent durable memory feature of Apache Ignite which can persist your cache data to the file disk to survive crash or restart so you can avoid data losing. 7 | * How to execute SQL queries over ignite caches 8 | * How to unit test and integration test ignite with spring boot 9 | * Simple Jenkins pipeline reference 10 | * How to do fail fast map reduce parallel jobs execution in sync and async way 11 | 12 | ## How it is integrated with spring boot : 13 | 14 | ![alt text](ignitedurablememory.jpg) 15 | 16 | ## How to handle parallel fail fast map reduce jobs in sync and async way: 17 | ![alt text](mapReduce.png) 18 | 19 | ## for more detailed technical information please check my post : 20 | Spring boot with Ignite durable memory and SQL queries : 21 | https://mromeh.com/2017/11/17/spring-boot-with-apache-ignite-persistent-durable-memory-storage-plus-sql-queries-over-ignite-cache/ 22 | 23 | And for fail fast map reduce jobs in ignite compute grid: 24 | https://mromeh.com/2017/12/18/spring-boot-with-apache-ignite-fail-fast-distributed-map-reduce-closures/ 25 | -------------------------------------------------------------------------------- /durable-memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Romeh/spring-boot-ignite/36ec93c2ef742cca467c74be65f20a1341232b0e/durable-memory.png -------------------------------------------------------------------------------- /ignitedurablememory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Romeh/spring-boot-ignite/36ec93c2ef742cca467c74be65f20a1341232b0e/ignitedurablememory.jpg -------------------------------------------------------------------------------- /mapReduce.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Romeh/spring-boot-ignite/36ec93c2ef742cca467c74be65f20a1341232b0e/mapReduce.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.romeh 7 | ignite-sample-manager 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | alert-manager 12 | Ignite Alert Manager 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.5.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | true 26 | true 27 | Dalston.SR2 28 | ${project.artifactId} 29 | LOCAL 30 | true 31 | true 32 | jacoco 33 | reuseReports 34 | ${project.basedir}/../target/jacoco.exec 35 | java 36 | 0 37 | ${project.basedir}/../target/jacoco.exec 38 | ${project.basedir}/../target/reports/jacoco 39 | true 40 | 3.3.1 41 | 2.6.0 42 | 2.0.4-SNAPSHOT 43 | 4.3.10.RELEASE 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-actuator 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-web 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-starter-thymeleaf 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-starter-aop 63 | 64 | 65 | org.projectlombok 66 | lombok 67 | true 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-starter-test 72 | test 73 | 74 | 75 | com.google.code.gson 76 | gson 77 | test 78 | 79 | 80 | org.apache.ignite 81 | ignite-core 82 | ${ignite.version} 83 | 84 | 85 | org.apache.ignite 86 | ignite-spring 87 | ${ignite.version} 88 | 89 | 90 | org.apache.ignite 91 | ignite-indexing 92 | ${ignite.version} 93 | 94 | 95 | com.h2database 96 | h2 97 | 98 | 99 | 100 | 101 | com.h2database 102 | h2 103 | 1.4.195 104 | 105 | 106 | org.apache.ignite 107 | ignite-slf4j 108 | ${ignite.version} 109 | 110 | 111 | io.springfox 112 | springfox-swagger2 113 | 2.7.0 114 | 115 | 116 | 117 | io.springfox 118 | springfox-swagger-ui 119 | 2.7.0 120 | 121 | 122 | 123 | ch.qos.logback 124 | logback-core 125 | 1.2.3 126 | 127 | 128 | ch.qos.logback 129 | logback-classic 130 | 1.2.3 131 | 132 | 133 | 134 | org.mockito 135 | mockito-core 136 | 2.8.47 137 | test 138 | 139 | 140 | 141 | org.springframework.retry 142 | spring-retry 143 | 1.2.1.RELEASE 144 | 145 | 146 | 147 | 148 | 149 | 150 | ${project.artifact.name}##${project.version}-${build.number} 151 | 152 | 153 | org.springframework.boot 154 | spring-boot-maven-plugin 155 | 156 | -Xmx164m -XX:MaxDirectMemorySize=912m 157 | 158 | 159 | 160 | 161 | build-info 162 | 163 | 164 | 165 | 166 | 167 | 168 | maven-failsafe-plugin 169 | 170 | ${integration-tests.skip} 171 | 172 | **/*IT.java 173 | 174 | 175 | com.romeh.ignitemanager.* 176 | 177 | 178 | 179 | 180 | 181 | integration-test 182 | verify 183 | 184 | 185 | 186 | 187 | 188 | 189 | maven-surefire-plugin 190 | 191 | ${unit-tests.skip} 192 | 193 | **/*IT.java 194 | com.romeh.ignitemanager.integration.* 195 | 196 | 197 | 198 | 199 | 200 | 201 | org.jacoco 202 | jacoco-maven-plugin 203 | 204 | ${sonar.jacoco.reportPaths} 205 | true 206 | 207 | 208 | 209 | default-prepare-agent 210 | 211 | prepare-agent 212 | 213 | 214 | 215 | default-report 216 | 217 | report 218 | 219 | 220 | ${jacoco.reporting.outputDirectory} 221 | 222 | 223 | 224 | default-report-aggregate 225 | verify 226 | 227 | report-aggregate 228 | 229 | 230 | 231 | ${jacoco.dataFile} 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | com.github.kongchen 240 | swagger-maven-plugin 241 | 3.1.0 242 | 243 | 244 | 245 | true 246 | com.romeh.ignitemanager.web.AlertsController 247 | service.url:8080 248 | / 249 | 250 | Alert manager swagger 251 | 0.0.1 252 | 253 | mahmoud.romih@romeh.com 254 | Mahmoud Romih 255 | 256 | 257 | http://www.apache.org/licenses/LICENSE-2.0.html 258 | Apache 2.0 259 | 260 | 261 | ${project.build.directory}/generated/swagger-ui 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | compile 270 | 271 | generate 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/AlertManagerApplication.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.retry.annotation.EnableRetry; 6 | import org.springframework.scheduling.annotation.EnableScheduling; 7 | 8 | 9 | @SpringBootApplication 10 | @EnableScheduling 11 | @EnableRetry 12 | public class AlertManagerApplication { 13 | 14 | public static void main(String[] args) { 15 | 16 | SpringApplication.run(AlertManagerApplication.class, args); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/SwaggerConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | import springfox.documentation.builders.PathSelectors; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.spi.DocumentationType; 9 | import springfox.documentation.spring.web.plugins.Docket; 10 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 11 | 12 | /** 13 | * Configuration class which enables Swagger 14 | * 15 | * @author romih 16 | */ 17 | @Configuration 18 | @EnableSwagger2 19 | public class SwaggerConfiguration { 20 | 21 | @Bean 22 | public Docket api() { 23 | return new Docket(DocumentationType.SWAGGER_2) 24 | .select() 25 | .apis(RequestHandlerSelectors.any()) 26 | .paths(PathSelectors.any()) 27 | .build(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/compute/DataGridCompute.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.compute; 2 | 3 | import java.util.Collection; 4 | import java.util.function.Consumer; 5 | 6 | import org.apache.ignite.Ignite; 7 | import org.apache.ignite.IgniteCompute; 8 | import org.apache.ignite.IgniteException; 9 | import org.apache.ignite.lang.IgniteCallable; 10 | import org.apache.ignite.lang.IgniteFuture; 11 | import org.apache.ignite.lang.IgniteReducer; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Component; 14 | 15 | /** 16 | * generic utility class for map reduce call 17 | */ 18 | @Component 19 | public class DataGridCompute { 20 | 21 | @Autowired 22 | private Ignite ignite; 23 | 24 | /** 25 | * @param jobs the list of jobs to be distributed into the data grid nodes from the master node 26 | * @param igniteReducer the ignite reducer which will be used to determine the reduction and collection logic 27 | * @param callback the callback to be invoked upon receiving the reduced final response 28 | * @param generic response type from the jobs 29 | * @param generic map reduced response type 30 | * @throws IgniteException 31 | * 32 | * a generic async map reduced call inside ignite compute grid 33 | */ 34 | public void executeMapReduceFailFast(Collection> jobs, IgniteReducer igniteReducer, Consumer callback) throws IgniteException { 35 | // you need to define your cluster group and if any defined in your data grid 36 | IgniteCompute igniteCompute = ignite.compute(ignite.cluster().forPredicate(clusterNode -> !clusterNode.isClient())); 37 | //execute the list of jobs in map reduce fashion and pass the custom reducer as well 38 | IgniteFuture future=igniteCompute.callAsync(jobs, igniteReducer); 39 | // then async listen for the result to invoke your post call back 40 | future.listen(result -> callback.accept(result.get())); 41 | } 42 | 43 | 44 | /** 45 | * @param jobs the list of jobs to be distributed into the data grid nodes from the master node 46 | * @param igniteReducer the ignite reducer which will be used to determine the reduction and collection logic 47 | * @param generic response type from the jobs 48 | * @param generic map reduced response type 49 | * @throws IgniteException 50 | * @return generic map reduced response type 51 | * a generic sync map reduced call inside ignite compute grid 52 | */ 53 | public E executeMapReduceFailFastSync(Collection> jobs, IgniteReducer igniteReducer) throws IgniteException { 54 | // you need to define your cluster group and if any defined in your data grid 55 | IgniteCompute igniteCompute = ignite.compute(ignite.cluster().forPredicate(clusterNode -> !clusterNode.isClient())); 56 | //execute the list of jobs in map reduce fashion and pass the custom reducer as well 57 | return igniteCompute.call(jobs, igniteReducer); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/compute/FailFastReducer.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.compute; 2 | 3 | 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | import org.apache.ignite.lang.IgniteReducer; 8 | import org.springframework.context.annotation.Scope; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * a fail fast map reducer to decide if it should keep waiting for other jobs to final reduce or it should terminate 13 | * and fail fast with the current responses if any failed 14 | */ 15 | @Component 16 | @Scope("prototype") 17 | public class FailFastReducer implements IgniteReducer { 18 | 19 | private final Map responseMap = new ConcurrentHashMap<>(); 20 | 21 | /** 22 | * @param serviceCallResponse the job response 23 | * @return return a boolean to decide it is time to reduce or not 24 | */ 25 | @Override 26 | public boolean collect(ServiceResponse serviceCallResponse) { 27 | if (serviceCallResponse != null) { 28 | if (serviceCallResponse.isSuccess()) { 29 | responseMap.put(serviceCallResponse.getServiceOrigin(), serviceCallResponse); 30 | return true; 31 | } else { 32 | responseMap.put(serviceCallResponse.getServiceOrigin(), serviceCallResponse); 33 | return false; 34 | } 35 | } 36 | return false; 37 | } 38 | 39 | /** 40 | * @return the final generic reduced response containing the list of jobs responses and global status 41 | */ 42 | @Override 43 | public MapReduceResponse reduce() { 44 | return MapReduceResponse.builder().success(checkStatus()).reducedResponses(responseMap).build(); 45 | } 46 | 47 | /** 48 | * @return the generic reduced response status based into the single status of each single collected jobs response 49 | */ 50 | public boolean checkStatus() { 51 | boolean status = true; 52 | for (Map.Entry key : responseMap.entrySet()) { 53 | status = status && responseMap.get(key.getKey()).isSuccess(); 54 | } 55 | return status; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/compute/MapReduceResponse.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.compute; 2 | 3 | import java.io.Serializable; 4 | import java.util.Map; 5 | 6 | import lombok.Builder; 7 | import lombok.EqualsAndHashCode; 8 | import lombok.Getter; 9 | import lombok.ToString; 10 | 11 | /** 12 | * the generic reduce response that contain all single collected jobs responses 13 | */ 14 | @Builder 15 | @Getter 16 | @ToString 17 | @EqualsAndHashCode 18 | public class MapReduceResponse implements Serializable { 19 | private Map reducedResponses; 20 | boolean success; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/compute/ServiceResponse.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.compute; 2 | 3 | 4 | import java.io.Serializable; 5 | 6 | import lombok.Builder; 7 | import lombok.EqualsAndHashCode; 8 | import lombok.Getter; 9 | import lombok.Setter; 10 | import lombok.ToString; 11 | 12 | /** 13 | * @param the service call response type 14 | */ 15 | @Getter 16 | @Setter 17 | @ToString 18 | @EqualsAndHashCode 19 | @Builder 20 | public class ServiceResponse implements Serializable { 21 | private T response; 22 | private boolean success ; 23 | private String serviceOrigin; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/config/AlertManagerConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.config; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collection; 5 | 6 | import org.apache.ignite.Ignite; 7 | import org.apache.ignite.IgniteException; 8 | import org.apache.ignite.Ignition; 9 | import org.apache.ignite.cache.CacheAtomicityMode; 10 | import org.apache.ignite.cache.CacheWriteSynchronizationMode; 11 | import org.apache.ignite.cluster.ClusterNode; 12 | import org.apache.ignite.configuration.BinaryConfiguration; 13 | import org.apache.ignite.configuration.CacheConfiguration; 14 | import org.apache.ignite.configuration.ConnectorConfiguration; 15 | import org.apache.ignite.configuration.DataRegionConfiguration; 16 | import org.apache.ignite.configuration.DataStorageConfiguration; 17 | import org.apache.ignite.configuration.IgniteConfiguration; 18 | import org.apache.ignite.configuration.MemoryConfiguration; 19 | import org.apache.ignite.configuration.PersistentStoreConfiguration; 20 | import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; 21 | import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; 22 | import org.springframework.beans.factory.annotation.Value; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | 26 | import com.romeh.ignitemanager.entities.AlertConfigEntry; 27 | import com.romeh.ignitemanager.entities.AlertEntry; 28 | import com.romeh.ignitemanager.entities.CacheNames; 29 | 30 | /** 31 | * Created by romeh on 16/08/2017. 32 | */ 33 | @Configuration 34 | public class AlertManagerConfiguration { 35 | 36 | @Value("${mail.service.baseUrl}") 37 | private String baseUrl; 38 | @Value("${mail.service.user}") 39 | private String user; 40 | @Value("${mail.service.password}") 41 | private String password; 42 | @Value("${enableFilePersistence}") 43 | private boolean enableFilePersistence; 44 | @Value("${igniteConnectorPort}") 45 | private int igniteConnectorPort; 46 | @Value("${igniteServerPortRange}") 47 | private String igniteServerPortRange; 48 | @Value("${ignitePersistenceFilePath}") 49 | private String ignitePersistenceFilePath; 50 | private static final String DATA_CONFIG_NAME = "MyDataRegionConfiguration"; 51 | 52 | @Bean 53 | IgniteConfiguration igniteConfiguration() { 54 | IgniteConfiguration igniteConfiguration = new IgniteConfiguration(); 55 | igniteConfiguration.setWorkDirectory(ignitePersistenceFilePath); 56 | igniteConfiguration.setClientMode(false); 57 | // durable file memory persistence 58 | if(enableFilePersistence){ 59 | 60 | DataStorageConfiguration dataStorageConfiguration = new DataStorageConfiguration(); 61 | dataStorageConfiguration.setStoragePath(ignitePersistenceFilePath + "/store"); 62 | dataStorageConfiguration.setWalArchivePath(ignitePersistenceFilePath + "/walArchive"); 63 | dataStorageConfiguration.setWalPath(ignitePersistenceFilePath + "/walStore"); 64 | dataStorageConfiguration.setPageSize(4 * 1024); 65 | DataRegionConfiguration dataRegionConfiguration = new DataRegionConfiguration(); 66 | dataRegionConfiguration.setName(DATA_CONFIG_NAME); 67 | dataRegionConfiguration.setInitialSize(100 * 1000 * 1000); 68 | dataRegionConfiguration.setMaxSize(200 * 1000 * 1000); 69 | dataRegionConfiguration.setPersistenceEnabled(true); 70 | dataStorageConfiguration.setDataRegionConfigurations(dataRegionConfiguration); 71 | igniteConfiguration.setDataStorageConfiguration(dataStorageConfiguration); 72 | igniteConfiguration.setConsistentId("RomehFileSystem"); 73 | } 74 | // connector configuration 75 | ConnectorConfiguration connectorConfiguration=new ConnectorConfiguration(); 76 | connectorConfiguration.setPort(igniteConnectorPort); 77 | // common ignite configuration 78 | igniteConfiguration.setMetricsLogFrequency(0); 79 | igniteConfiguration.setQueryThreadPoolSize(2); 80 | igniteConfiguration.setDataStreamerThreadPoolSize(1); 81 | igniteConfiguration.setManagementThreadPoolSize(2); 82 | igniteConfiguration.setPublicThreadPoolSize(2); 83 | igniteConfiguration.setSystemThreadPoolSize(2); 84 | igniteConfiguration.setRebalanceThreadPoolSize(1); 85 | igniteConfiguration.setAsyncCallbackPoolSize(2); 86 | igniteConfiguration.setPeerClassLoadingEnabled(false); 87 | igniteConfiguration.setIgniteInstanceName("alertsGrid"); 88 | BinaryConfiguration binaryConfiguration = new BinaryConfiguration(); 89 | binaryConfiguration.setCompactFooter(false); 90 | igniteConfiguration.setBinaryConfiguration(binaryConfiguration); 91 | // cluster tcp configuration 92 | TcpDiscoverySpi tcpDiscoverySpi=new TcpDiscoverySpi(); 93 | TcpDiscoveryVmIpFinder tcpDiscoveryVmIpFinder=new TcpDiscoveryVmIpFinder(); 94 | // need to be changed when it come to real cluster 95 | tcpDiscoveryVmIpFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47509")); 96 | tcpDiscoverySpi.setIpFinder(tcpDiscoveryVmIpFinder); 97 | igniteConfiguration.setDiscoverySpi(new TcpDiscoverySpi()); 98 | // cache configuration 99 | CacheConfiguration alerts=new CacheConfiguration(); 100 | alerts.setCopyOnRead(false); 101 | // as we have one node for now 102 | alerts.setBackups(1); 103 | alerts.setAtomicityMode(CacheAtomicityMode.ATOMIC); 104 | alerts.setName(CacheNames.Alerts.name()); 105 | alerts.setDataRegionName(DATA_CONFIG_NAME); 106 | alerts.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_ASYNC); 107 | alerts.setIndexedTypes(String.class,AlertEntry.class); 108 | 109 | CacheConfiguration alertsConfig=new CacheConfiguration(); 110 | alertsConfig.setCopyOnRead(false); 111 | // as we have one node for now 112 | alertsConfig.setBackups(1); 113 | alertsConfig.setAtomicityMode(CacheAtomicityMode.ATOMIC); 114 | alertsConfig.setName(CacheNames.AlertsConfig.name()); 115 | alertsConfig.setIndexedTypes(String.class,AlertConfigEntry.class); 116 | alertsConfig.setDataRegionName(DATA_CONFIG_NAME); 117 | alertsConfig.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_ASYNC); 118 | igniteConfiguration.setCacheConfiguration(alerts,alertsConfig); 119 | return igniteConfiguration; 120 | } 121 | 122 | @Bean(destroyMethod = "close") 123 | Ignite ignite(IgniteConfiguration igniteConfiguration) throws IgniteException { 124 | final Ignite ignite = Ignition.start(igniteConfiguration); 125 | // Activate the cluster. Automatic topology initialization occurs 126 | // only if you manually activate the cluster for the very first time. 127 | ignite.cluster().active(true); 128 | /*// Get all server nodes that are already up and running. 129 | Collection nodes = ignite.cluster().forServers().nodes(); 130 | // Set the baseline topology that is represented by these nodes. 131 | ignite.cluster().setBaselineTopology(nodes);*/ 132 | return ignite; 133 | } 134 | 135 | 136 | 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/entities/AlertConfigEntry.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.entities; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | import org.apache.ignite.cache.query.annotations.QuerySqlField; 9 | 10 | import io.swagger.annotations.ApiModelProperty; 11 | import lombok.EqualsAndHashCode; 12 | import lombok.Getter; 13 | import lombok.Setter; 14 | import lombok.ToString; 15 | 16 | /** 17 | * Created by romeh on 08/08/2017. 18 | */ 19 | @EqualsAndHashCode 20 | @Getter 21 | @Setter 22 | @ToString 23 | public class AlertConfigEntry implements Serializable { 24 | @ApiModelProperty(notes = "alert service code required to be entered by user into REST API ", required = true) 25 | @QuerySqlField(index = true) 26 | @NotNull 27 | String serviceCode; 28 | @ApiModelProperty(notes = "alert error code required to be entered by user into REST API ", required = true) 29 | @QuerySqlField(index = true) 30 | @NotNull 31 | String errorCode; 32 | List emails; 33 | int maxCount; 34 | String mailTemplate; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/entities/AlertEntry.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.entities; 2 | 3 | import java.io.Serializable; 4 | import java.util.Map; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | import org.apache.ignite.cache.query.annotations.QuerySqlField; 9 | 10 | import io.swagger.annotations.ApiModelProperty; 11 | import lombok.AllArgsConstructor; 12 | import lombok.Builder; 13 | import lombok.EqualsAndHashCode; 14 | import lombok.Getter; 15 | import lombok.NoArgsConstructor; 16 | import lombok.Setter; 17 | import lombok.ToString; 18 | 19 | /** 20 | * Created by romeh on 19/07/2017. 21 | */ 22 | @Builder 23 | @Getter 24 | @Setter 25 | @ToString 26 | @EqualsAndHashCode 27 | @AllArgsConstructor 28 | @NoArgsConstructor 29 | public class AlertEntry implements Serializable { 30 | @ApiModelProperty(notes = "the key value alert content for error description required to be entered by user into REST API ") 31 | private Map alertContent; 32 | @ApiModelProperty(notes = "alert error code required to be entered by user into REST API ", required = true) 33 | @QuerySqlField(index = true) 34 | @NotNull 35 | private String errorCode; 36 | @ApiModelProperty(notes = "alert service code required to be entered by user into REST API ", required = true) 37 | @QuerySqlField(index = true) 38 | @NotNull 39 | private String serviceId; 40 | @ApiModelProperty(notes = "alert severity required to be entered by user into REST API ", required = true) 41 | @NotNull 42 | private String severity; 43 | @QuerySqlField(index = true) 44 | private Long timestamp; 45 | private String alertId; 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/entities/AlertHolder.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.entities; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import lombok.Builder; 7 | import lombok.EqualsAndHashCode; 8 | import lombok.Getter; 9 | import lombok.Setter; 10 | import lombok.ToString; 11 | 12 | /** 13 | * Created by romeh on 09/08/2017. 14 | */ 15 | @EqualsAndHashCode 16 | @ToString 17 | @Getter 18 | @Setter 19 | @Builder 20 | public class AlertHolder implements Serializable { 21 | private String serviceCode; 22 | private List alerts; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/entities/AlertsConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.entities; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import lombok.Getter; 9 | import lombok.Setter; 10 | 11 | /** 12 | * Created by romeh on 08/08/2017. 13 | */ 14 | @Configuration 15 | @ConfigurationProperties(prefix = "alerts") 16 | @Getter 17 | @Setter 18 | public class AlertsConfiguration { 19 | 20 | private List alertConfigurations; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/entities/CacheNames.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.entities; 2 | 3 | /** 4 | * Created by romeh on 09/08/2017. 5 | */ 6 | public enum CacheNames { 7 | AlertsConfig, 8 | Alerts 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/entities/CustomErrorResponse.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.entities; 2 | 3 | import java.io.Serializable; 4 | 5 | import lombok.AllArgsConstructor; 6 | import lombok.Getter; 7 | 8 | /** 9 | * {@link CustomErrorResponse} will be returned in case of custom error occurrence 10 | * Necessary for proper Swagger documentation. 11 | * 12 | * @author romih 13 | */ 14 | @SuppressWarnings("unused") 15 | @AllArgsConstructor 16 | @Getter 17 | public class CustomErrorResponse implements Serializable { 18 | 19 | private static final long serialVersionUID = -7755563009111273632L; 20 | 21 | private String errorCode; 22 | 23 | private String errorMessage; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/exception/ResourceNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.exception; 2 | 3 | /** 4 | * This exception should be thrown in all cases when a resource cannot be found 5 | * 6 | * @author romih 7 | */ 8 | public class ResourceNotFoundException extends RuntimeException { 9 | 10 | /** 11 | * Instantiates a new {@link ResourceNotFoundException}. 12 | * 13 | * @param message the message 14 | */ 15 | public ResourceNotFoundException(final String message) { 16 | super(message); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/repositories/AlertsConfigStore.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.repositories; 2 | 3 | import java.util.Optional; 4 | 5 | import com.romeh.ignitemanager.entities.AlertConfigEntry; 6 | 7 | /** 8 | * Created by romeh on 18/08/2017. 9 | */ 10 | public interface AlertsConfigStore { 11 | AlertConfigEntry getConfigForServiceIdCodeId(String serviceId, String codeId); 12 | void update(String serviceId, String codeId, AlertConfigEntry alertConfigEntry); 13 | Optional getConfigForServiceIdCodeIdCount(String serviceId, String codeId); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/repositories/AlertsStore.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.repositories; 2 | 3 | import java.util.List; 4 | 5 | import com.romeh.ignitemanager.entities.AlertEntry; 6 | 7 | /** 8 | * Created by romeh on 08/08/2017. 9 | */ 10 | public interface AlertsStore { 11 | List getAlertForServiceId(String serviceId); 12 | 13 | void updateAlertEntry(String serviceId, String errorCode, AlertEntry alertEntry); 14 | 15 | List getAllAlerts(); 16 | 17 | void deleteAlertEntry(String alertId); 18 | 19 | void createAlertEntry(AlertEntry alertEntry); 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/repositories/impl/IgniteAlertConfigStore.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.repositories.impl; 2 | 3 | import java.util.Optional; 4 | 5 | import javax.cache.Cache; 6 | 7 | import org.apache.ignite.Ignite; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Component; 12 | 13 | import com.romeh.ignitemanager.entities.AlertConfigEntry; 14 | import com.romeh.ignitemanager.entities.CacheNames; 15 | import com.romeh.ignitemanager.exception.ResourceNotFoundException; 16 | import com.romeh.ignitemanager.repositories.AlertsConfigStore; 17 | 18 | /** 19 | * Created by romeh on 18/08/2017. 20 | */ 21 | @Component 22 | public class IgniteAlertConfigStore implements AlertsConfigStore { 23 | 24 | private static final Logger logger = LoggerFactory.getLogger(IgniteAlertConfigStore.class); 25 | 26 | @Autowired 27 | private Ignite ignite; 28 | 29 | @Override 30 | public AlertConfigEntry getConfigForServiceIdCodeId(String serviceId, String codeId) { 31 | return Optional.ofNullable(getAlertsConfigCache().get(serviceId + "_" + codeId)) 32 | .orElseThrow(() -> new ResourceNotFoundException(String.format("Alert config for %s with %s not found", serviceId,codeId))); 33 | } 34 | 35 | @Override 36 | public void update(String serviceId, String codeId, AlertConfigEntry alertConfigEntry) { 37 | getAlertsConfigCache().put(serviceId + "_" + codeId, alertConfigEntry); 38 | } 39 | 40 | @Override 41 | public Optional getConfigForServiceIdCodeIdCount(String serviceId, String codeId) { 42 | return Optional.ofNullable(getAlertsConfigCache().get(serviceId + "_" + codeId)); 43 | 44 | } 45 | 46 | 47 | public Cache getAlertsConfigCache() { 48 | return ignite.getOrCreateCache(CacheNames.AlertsConfig.name()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/repositories/impl/IgniteAlertsStore.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.repositories.impl; 2 | 3 | import java.util.Collections; 4 | import java.util.List; 5 | import java.util.Optional; 6 | import java.util.stream.Collectors; 7 | 8 | import javax.cache.Cache; 9 | 10 | import org.apache.ignite.Ignite; 11 | import org.apache.ignite.IgniteCache; 12 | import org.apache.ignite.cache.query.SqlFieldsQuery; 13 | import org.apache.ignite.cache.query.SqlQuery; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.scheduling.annotation.Async; 18 | import org.springframework.stereotype.Component; 19 | 20 | import com.romeh.ignitemanager.entities.AlertConfigEntry; 21 | import com.romeh.ignitemanager.entities.AlertEntry; 22 | import com.romeh.ignitemanager.entities.CacheNames; 23 | import com.romeh.ignitemanager.exception.ResourceNotFoundException; 24 | import com.romeh.ignitemanager.repositories.AlertsConfigStore; 25 | import com.romeh.ignitemanager.repositories.AlertsStore; 26 | import com.romeh.ignitemanager.services.MailService; 27 | 28 | @Component 29 | public class IgniteAlertsStore implements AlertsStore { 30 | private static final Logger logger = LoggerFactory.getLogger(IgniteAlertsStore.class); 31 | 32 | @Autowired 33 | private Ignite ignite; 34 | 35 | @Autowired 36 | private MailService mailService; 37 | 38 | @Autowired 39 | private AlertsConfigStore alertsConfigStore; 40 | 41 | @Override 42 | public List getAlertForServiceId(String serviceId) { 43 | final String sql = "serviceId = ?"; 44 | SqlQuery query = new SqlQuery<>(AlertEntry.class, sql); 45 | query.setArgs(serviceId); 46 | return Optional.ofNullable(getAlertsCache().query(query).getAll() 47 | .stream() 48 | .map(Cache.Entry::getValue) 49 | .collect(Collectors.toList())) 50 | .orElseThrow(() -> new ResourceNotFoundException(String.format("Alert for %s not found", serviceId))); 51 | } 52 | 53 | @Override 54 | public void updateAlertEntry(String serviceId, String serviceCode, AlertEntry alertEntry) { 55 | final IgniteCache alertsCache = getAlertsCache(); 56 | // update the alert entry via cache invoke for atomicity 57 | alertsCache.invoke(alertEntry.getAlertId(), (mutableEntry, objects) -> { 58 | if (mutableEntry.exists() && mutableEntry.getValue() != null) { 59 | logger.debug("updating alert entry into the cache store invoke: {},{}", serviceId, serviceCode); 60 | mutableEntry.setValue(alertEntry); 61 | } else { 62 | throw new ResourceNotFoundException(String.format("Alert for %s with %s not found", serviceId, serviceCode)); 63 | } 64 | // by api design nothing needed here 65 | return null; 66 | }); 67 | } 68 | 69 | @Override 70 | public List getAllAlerts() { 71 | final String sql = "select * from AlertEntry"; 72 | SqlQuery query = new SqlQuery<>(AlertEntry.class, sql); 73 | return getAlertsCache().query(query).getAll() 74 | .stream() 75 | .map(Cache.Entry::getValue) 76 | .collect(Collectors.toList()); 77 | 78 | } 79 | 80 | @Override 81 | public void deleteAlertEntry(String alertId) { 82 | final IgniteCache alertsCache = getAlertsCache(); 83 | alertsCache.remove(alertId); 84 | } 85 | 86 | @Override 87 | public void createAlertEntry(AlertEntry alertEntry) { 88 | // get the alert config if any 89 | final Optional configForServiceIdCodeIdCount = 90 | alertsConfigStore.getConfigForServiceIdCodeIdCount(alertEntry.getServiceId(), alertEntry.getErrorCode()); 91 | // get the max count of alerts before sending mail 92 | final int maxCount = configForServiceIdCodeIdCount.isPresent() ? 93 | configForServiceIdCodeIdCount.get().getMaxCount() : 1; 94 | final String mailTemplate = configForServiceIdCodeIdCount.isPresent() ? 95 | configForServiceIdCodeIdCount.get().getMailTemplate() : "ticket"; 96 | // define the expiry of the entry in the cache 97 | final IgniteCache alertsCache = getAlertsCache(); 98 | // insert into the key value store 99 | alertsCache.put(alertEntry.getAlertId(), alertEntry); 100 | // send the mail notification if max is there 101 | final SqlFieldsQuery sql = new SqlFieldsQuery("select count(*) from AlertEntry where serviceId = '" + alertEntry.getServiceId() + "' and errorCode = '" + alertEntry.getErrorCode() + "'"); 102 | final List> count = alertsCache.query(sql).getAll(); 103 | if (count != null && !count.isEmpty()) { 104 | final Long result = (Long) count.get(0).get(0); 105 | if (result >= maxCount) { 106 | logger.debug("max alerts count is reached for : {}, start sending mail alert {}", alertEntry.toString()); 107 | sendMail(alertEntry, configForServiceIdCodeIdCount.isPresent() ? configForServiceIdCodeIdCount.get().getEmails() : Collections.emptyList(), mailTemplate); 108 | } 109 | } 110 | } 111 | 112 | @Async 113 | protected void sendMail(AlertEntry alertEntry, List emails, String mailTemplate) { 114 | // send the mail then delete the entry 115 | final boolean doneSending = mailService.sendAlert(alertEntry, emails, mailTemplate); 116 | if (doneSending) { 117 | cleanAllAlertEntriesForThatErrorCodeAndServiceCode(alertEntry.getServiceId(), alertEntry.getErrorCode()); 118 | } 119 | 120 | } 121 | // clean all alerts for that service code and service id 122 | private void cleanAllAlertEntriesForThatErrorCodeAndServiceCode(String serviceId, String errorId) { 123 | 124 | // commenting it out for testing without clean-up 125 | /* // query the matching records first 126 | final String sql = "serviceId = ? and errorCode= ?"; 127 | SqlQuery query = new SqlQuery(AlertEntry.class,sql); 128 | query.setArgs(serviceId, errorId); 129 | final List> to_Delete_Alerts = getAlertsCache().query(query).getAll(); 130 | // then call remove all as this will remove the records from the cache and the persistent file system 131 | // as sql delete will just delete it from the cache layer not the file system 132 | // or the persistent store 133 | if(to_Delete_Alerts!=null && !to_Delete_Alerts.isEmpty()){ 134 | getAlertsCache().removeAll(new HashSet(to_Delete_Alerts.stream().map(stringAlertEntryEntry -> stringAlertEntryEntry.getKey()).collect(Collectors.toList()))); 135 | }*/ 136 | 137 | } 138 | 139 | // get alerts cache store 140 | protected IgniteCache getAlertsCache() { 141 | return ignite.cache(CacheNames.Alerts.name()); 142 | } 143 | 144 | 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/services/AlertsService.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.services; 2 | 3 | import java.util.List; 4 | import java.util.UUID; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import com.romeh.ignitemanager.entities.AlertConfigEntry; 12 | import com.romeh.ignitemanager.entities.AlertEntry; 13 | import com.romeh.ignitemanager.repositories.AlertsConfigStore; 14 | import com.romeh.ignitemanager.repositories.AlertsStore; 15 | 16 | /** 17 | * Created by romeh on 09/08/2017. 18 | */ 19 | @Service 20 | public class AlertsService { 21 | private static final Logger logger = LoggerFactory.getLogger(AlertsService.class); 22 | 23 | @Autowired 24 | private AlertsStore alertsStore; 25 | 26 | @Autowired 27 | private AlertsConfigStore alertsConfigStore; 28 | 29 | 30 | public void createAlertEntry(AlertEntry alertEntry) { 31 | logger.debug("createAlertEntry service call with {}",alertEntry.toString()); 32 | alertEntry.setAlertId(UUID.randomUUID().toString()); 33 | alertEntry.setTimestamp(System.currentTimeMillis()); 34 | alertsStore.createAlertEntry(alertEntry); 35 | 36 | } 37 | 38 | public List getAlertForServiceId(String serviceId) { 39 | logger.debug("GetAlertForServiceId service call with {}",serviceId); 40 | return alertsStore.getAlertForServiceId(serviceId); 41 | } 42 | 43 | 44 | public void updateAlertEntry(String serviceId, String serviceCode, AlertEntry alertEntry) { 45 | logger.debug("updateAlertEntry service call with {}, {}, {}",serviceId,serviceCode,alertEntry.toString()); 46 | alertsStore.updateAlertEntry(serviceId, serviceCode, alertEntry); 47 | } 48 | 49 | 50 | public List getAllAlerts() { 51 | logger.debug("getAllAlerts service call"); 52 | return alertsStore.getAllAlerts(); 53 | } 54 | 55 | 56 | public void deleteAlertEntry(String alertId) { 57 | logger.debug("deleteAlertEntry service call: {}, {}",alertId); 58 | alertsStore.deleteAlertEntry(alertId); 59 | } 60 | 61 | 62 | public AlertConfigEntry getConfigForServiceIdCodeId(String serviceId, String codeId) { 63 | logger.debug("getConfigForServiceIdCodeId service call: {},{}",serviceId,codeId); 64 | return alertsConfigStore.getConfigForServiceIdCodeId(serviceId, codeId); 65 | } 66 | 67 | 68 | public void updateAlertConfig(String serviceId, String codeId, AlertConfigEntry alertConfigEntry) { 69 | logger.debug("updateAlertConfig service call: {}, {}, {}",serviceId,codeId,alertConfigEntry.toString()); 70 | alertsConfigStore.update(serviceId, codeId, alertConfigEntry); 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/services/CleanExpiredAlertsService.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.services; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ignite.Ignite; 6 | import org.apache.ignite.IgniteCache; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.scheduling.annotation.Scheduled; 11 | import org.springframework.stereotype.Service; 12 | 13 | import com.romeh.ignitemanager.entities.AlertEntry; 14 | import com.romeh.ignitemanager.entities.CacheNames; 15 | 16 | /** 17 | * Created by romeh on 22/08/2017. 18 | */ 19 | @Service 20 | public class CleanExpiredAlertsService { 21 | private static final Logger logger = LoggerFactory.getLogger(CleanExpiredAlertsService.class); 22 | 23 | 24 | @Autowired 25 | Ignite ignite; 26 | 27 | @Scheduled(initialDelayString = "${initialDelay}", fixedDelayString = "${fixedDelay}") 28 | // un comment if u need to test it otherwise it will clear ur alerts cache during startup 29 | public void cleanExpiredRecords() { 30 | /* // query the matching records first 31 | logger.debug("Starting the clean up job to clear the expired records"); 32 | long towMinutesRange = System.currentTimeMillis()-900000; 33 | final IgniteCache> alertsCache = getAlertsCache(); 34 | final String sql = "select * from AlertEntry where timestamp <= ?"; 35 | SqlQuery query = new SqlQuery(AlertEntry.class,sql); 36 | query.setArgs(towMinutesRange); 37 | final List> toDeleteAlerts = alertsCache.query(query).getAll(); 38 | // then call remove all as this will remove the records from the cache and the persistent file system as sql delete will just delete it from the cache layer not the file system 39 | // or the persistent store 40 | if(toDeleteAlerts!=null && !toDeleteAlerts.isEmpty()){ 41 | logger.debug("Finished cleaning out {} records",toDeleteAlerts.size()); 42 | alertsCache.removeAll(new HashSet(toDeleteAlerts 43 | .stream() 44 | .map(Cache.Entry::getKey) 45 | .collect(Collectors.toList()))); 46 | 47 | }*/ 48 | 49 | } 50 | 51 | // get alerts cache store 52 | protected IgniteCache> getAlertsCache() { 53 | 54 | return ignite.cache(CacheNames.Alerts.name()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/services/ComputeService.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.services; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ignite.lang.IgniteCallable; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import com.romeh.ignitemanager.compute.DataGridCompute; 12 | import com.romeh.ignitemanager.compute.FailFastReducer; 13 | import com.romeh.ignitemanager.compute.MapReduceResponse; 14 | import com.romeh.ignitemanager.compute.ServiceResponse; 15 | 16 | /** 17 | * sample service for how to call map reduce jobs in parallel asynchronous with fail fast reducer 18 | */ 19 | @Service 20 | public class ComputeService { 21 | 22 | private static final Logger logger = LoggerFactory.getLogger(AlertsService.class); 23 | private final DataGridCompute dataGridCompute; 24 | @Autowired 25 | private FailFastReducer failFastReducer; 26 | 27 | @Autowired 28 | public ComputeService(DataGridCompute dataGridCompute) { 29 | this.dataGridCompute = dataGridCompute; 30 | } 31 | 32 | /** 33 | * call to ignite compute grid with list if jobs in parallel asynchronous 34 | */ 35 | public void validateWithAllServicesInParallelAsync(List> jobs){ 36 | // execute the jobs with the fail fast reducer in parallel and async the just log the final aggregated response 37 | dataGridCompute.executeMapReduceFailFast(jobs,failFastReducer, 38 | mapReduceResponse -> logger.debug(mapReduceResponse.toString())); 39 | 40 | } 41 | /** 42 | * call to ignite compute grid with list if jobs in parallel synchronous 43 | */ 44 | public MapReduceResponse validateWithAllServicesInParallelSync(List> jobs){ 45 | // execute the jobs with the fail fast reducer in parallel and sync the just log the final aggregated response 46 | return dataGridCompute.executeMapReduceFailFastSync(jobs,failFastReducer); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/services/DataLoaderService.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.services; 2 | 3 | import javax.annotation.PostConstruct; 4 | import javax.cache.Cache; 5 | 6 | import org.apache.ignite.Ignite; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import com.romeh.ignitemanager.entities.AlertConfigEntry; 11 | import com.romeh.ignitemanager.entities.AlertsConfiguration; 12 | import com.romeh.ignitemanager.entities.CacheNames; 13 | 14 | /** 15 | * Created by romeh on 11/08/2017. 16 | */ 17 | @Service 18 | public class DataLoaderService { 19 | 20 | 21 | @Autowired 22 | private Ignite ignite; 23 | 24 | @Autowired 25 | private AlertsConfiguration alertsConfig; 26 | 27 | 28 | @PostConstruct 29 | public void init() { 30 | final Cache alertsConfigCache = ignite.getOrCreateCache(CacheNames.AlertsConfig.name()); 31 | this.alertsConfig.getAlertConfigurations().forEach(alertConfigEntity -> { 32 | alertsConfigCache.putIfAbsent(alertConfigEntity.getServiceCode() + "_" + alertConfigEntity.getErrorCode(), 33 | alertConfigEntity); 34 | 35 | }); 36 | 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/services/MailService.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.services; 2 | 3 | import java.util.List; 4 | import java.util.Locale; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.beans.factory.annotation.Value; 10 | import org.springframework.retry.annotation.CircuitBreaker; 11 | import org.springframework.retry.annotation.Recover; 12 | import org.springframework.stereotype.Service; 13 | import org.thymeleaf.TemplateEngine; 14 | import org.thymeleaf.context.Context; 15 | 16 | import com.romeh.ignitemanager.entities.AlertEntry; 17 | import com.romeh.ignitemanager.exception.ResourceNotFoundException; 18 | 19 | //import org.springframework.retry.annotation.CircuitBreaker; 20 | //import org.springframework.retry.annotation.Recover; 21 | 22 | /** 23 | * Created by romeh on 17/08/2017. 24 | */ 25 | @Service 26 | public class MailService { 27 | private static final Logger logger = LoggerFactory.getLogger(MailService.class); 28 | 29 | @Value("${mail.service.undeliverable}") 30 | private String undeliverable; 31 | 32 | @Value("${mail.from.bpo.mailbox}") 33 | private String mailFromBpo; 34 | 35 | @Value("${mail.default.receiver}") 36 | private String defaultReceiver; 37 | 38 | @Autowired 39 | private TemplateEngine templateEngine; 40 | 41 | 42 | 43 | public boolean sendAlert(AlertEntry alertEntry, List emails, String mailTemplate){ 44 | logger.debug("Sending alert e-mail to '{}'", emails.toString()); 45 | Locale locale = Locale.getDefault(); 46 | Context context = new Context(locale); 47 | context.setVariable("alert", alertEntry); 48 | String content = templateEngine.process(mailTemplate, context); 49 | return sendEmail(mailFromBpo, emails, "New Application ticket has been created", content); 50 | } 51 | 52 | @CircuitBreaker(maxAttempts = 2, openTimeout = 5000l, resetTimeout = 10000l,exclude = ResourceNotFoundException.class) 53 | public boolean sendEmail(String from, List to, String subject, String content) { 54 | logger.debug("Sending to {}",to.toString()); 55 | return true; 56 | 57 | } 58 | 59 | 60 | 61 | 62 | /** 63 | * The recover method needs to have same return type and parameters. 64 | * 65 | * @return 66 | */ 67 | @Recover 68 | private boolean fallbackForCall() { 69 | logger.error("Fallback for mail service call invoked, mail service is NOT reachable"); 70 | return false; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/web/AlertsController.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.web; 2 | 3 | import java.util.List; 4 | 5 | import javax.validation.Valid; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.http.HttpStatus; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestBody; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestMethod; 15 | import org.springframework.web.bind.annotation.ResponseBody; 16 | import org.springframework.web.bind.annotation.ResponseStatus; 17 | import org.springframework.web.bind.annotation.RestController; 18 | 19 | import com.romeh.ignitemanager.entities.AlertEntry; 20 | import com.romeh.ignitemanager.services.AlertsService; 21 | 22 | /** 23 | * Created by romeh on 08/08/2017. 24 | */ 25 | @RestController 26 | @RequestMapping("/alerts") 27 | public class AlertsController { 28 | 29 | private static final Logger LOGGER = LoggerFactory.getLogger(AlertsController.class); 30 | 31 | @Autowired 32 | private AlertsService alertsService; 33 | 34 | @RequestMapping(value = "/{serviceId}", method = RequestMethod.GET, produces = "application/json") 35 | @ResponseBody 36 | public List getServiceAlerts(@PathVariable final String serviceId) { 37 | LOGGER.debug("Trying to retrieve alerts by ID: {}", serviceId); 38 | return alertsService.getAlertForServiceId(serviceId); 39 | } 40 | 41 | 42 | @RequestMapping(method = RequestMethod.GET, produces = "application/json") 43 | @ResponseBody 44 | public List getAllAlerts() { 45 | LOGGER.debug("Trying to retrieve all alerts"); 46 | return alertsService.getAllAlerts(); 47 | 48 | } 49 | 50 | 51 | @RequestMapping(method = RequestMethod.POST, produces = "application/json") 52 | @ResponseBody 53 | @ResponseStatus(HttpStatus.CREATED) 54 | public void createAlert(@Valid @RequestBody AlertEntry request) { 55 | LOGGER.debug("Trying to create a alert: {}", request.toString()); 56 | alertsService.createAlertEntry(request); 57 | } 58 | 59 | 60 | @RequestMapping(value = "/{serviceId}/{errorCodeId}", method = RequestMethod.PUT, produces = "application/json") 61 | @ResponseBody 62 | @ResponseStatus(HttpStatus.OK) 63 | public void updateAlert(@PathVariable final String serviceId, @PathVariable final String errorCodeId, 64 | @Valid @RequestBody AlertEntry request) { 65 | LOGGER.debug("Trying to update a alert: {}", request.toString()); 66 | alertsService.updateAlertEntry(serviceId, errorCodeId, request); 67 | 68 | } 69 | 70 | 71 | @RequestMapping(value = "/{alertId}", method = RequestMethod.DELETE, produces = "application/json") 72 | @ResponseBody 73 | @ResponseStatus(HttpStatus.OK) 74 | public void deleteAlert(@PathVariable final String alertId) { 75 | LOGGER.debug("Trying to delete a alert: {},{}", alertId); 76 | alertsService.deleteAlertEntry(alertId); 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/romeh/ignitemanager/web/ErrorHandler.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.web; 2 | 3 | 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.ResponseStatus; 11 | 12 | import com.romeh.ignitemanager.entities.CustomErrorResponse; 13 | import com.romeh.ignitemanager.exception.ResourceNotFoundException; 14 | 15 | /** 16 | * Generic error handling mechanism. 17 | * 18 | * @author romih 19 | */ 20 | @ControllerAdvice 21 | public class ErrorHandler { 22 | 23 | private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandler.class); 24 | 25 | private enum ERROR_CODE { 26 | E0001 27 | } 28 | 29 | @ResponseStatus(HttpStatus.NOT_FOUND) // 404 30 | @ExceptionHandler(ResourceNotFoundException.class) 31 | @ResponseBody 32 | public CustomErrorResponse handleNotFound(ResourceNotFoundException ex) { 33 | LOGGER.warn("Entity was not found", ex); 34 | return new CustomErrorResponse(ERROR_CODE.E0001.name(), ex.getMessage()); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: romehAlertManager 4 | jackson: 5 | default-property-inclusion: non_null 6 | 7 | mail: 8 | service: 9 | baseUrl: http://romeh-wsitt.com/Mail/soap/mail 10 | user: romehALERT 11 | password: romehALERT 12 | undeliverable: undeliverable@belgacom-ics.com 13 | from: 14 | bpo.mailbox: romeh.alerts@romeh.com 15 | default: 16 | receiver: mahmoud.romih@romeh.com 17 | 18 | alerts: 19 | alertConfigurations: 20 | - 21 | serviceCode: "CODE_TWO" 22 | errorCode: "1001" 23 | emails: 24 | - "mahmoud.romih@romeh.com" 25 | maxCount: "3" 26 | mailTemplate: "ticket" 27 | - 28 | serviceCode: "CODE_ONE" 29 | errorCode: "1002" 30 | emails: 31 | - "mahmoud.romih@romeh.com" 32 | - "mahmoud.romih@romeh.com" 33 | maxCount: "2" 34 | mailTemplate: "ticket" 35 | 36 | enableFilePersistence: true 37 | igniteConnectorPort: 11211 38 | igniteServerPortRange: 47500..47509 39 | ignitePersistenceFilePath: /Users/romeh/github/spring-boot-ignite/data 40 | initialDelay: 10000 41 | fixedDelay: 60000 42 | 43 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ${FILE_LOG_PATTERN} 10 | utf8 11 | 12 | 13 | 14 | 15 | ${LOG_HOME}/alertManager.log 16 | true 17 | 18 | ${FILE_LOG_PATTERN} 19 | 20 | 21 | debug.log.%i 22 | 23 | 25 | 10MB 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/main/resources/templates/ticket.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Ticket has been created 5 | 6 | 7 | 8 | Dears, 9 |
10 |
11 | The support team inform you that your application with service Id: has created a ticket with a severity 12 | . 14 |
15 |
16 | Please find below all the details related to the ticket: 17 |
18 |
    19 |
  • Error code:
  • 20 |
  • Ticket details: 21 |
      22 |
    • 23 | : 24 |
      25 |
    • 26 |
  • 27 | 28 |
29 |
30 | We kindly ask you to take the necessary actions as you are part of the informing mail list of this ticket type. 31 |
32 |
33 | Kind regards, 34 |
35 | The Support team 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/test/java/com/romeh/ignitemanager/integration/AlertManagerApplicationIT.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.integration; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.net.URL; 7 | import java.util.Arrays; 8 | 9 | import org.apache.ignite.lang.IgniteCallable; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.boot.test.context.SpringBootTest; 15 | import org.springframework.boot.test.web.client.TestRestTemplate; 16 | import org.springframework.boot.web.server.LocalServerPort; 17 | import org.springframework.test.context.ActiveProfiles; 18 | import org.springframework.test.context.junit4.SpringRunner; 19 | 20 | import com.romeh.ignitemanager.AlertManagerApplication; 21 | import com.romeh.ignitemanager.compute.MapReduceResponse; 22 | import com.romeh.ignitemanager.compute.ServiceResponse; 23 | import com.romeh.ignitemanager.services.ComputeService; 24 | 25 | @RunWith(SpringRunner.class) 26 | @SpringBootTest(classes = AlertManagerApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 27 | @ActiveProfiles("INTEGRATION_TEST") 28 | public class AlertManagerApplicationIT { 29 | 30 | @LocalServerPort 31 | private int port; 32 | @Autowired 33 | private TestRestTemplate template; 34 | @Autowired 35 | ComputeService computeService; 36 | private URL base; 37 | 38 | @Before 39 | public void setUp() throws Exception { 40 | this.base = new URL("http://localhost:" + port + "/"); 41 | } 42 | 43 | @Test 44 | public void contextLoads() { 45 | assertTrue(template.getForEntity(base+"/health",String.class).getStatusCode().is2xxSuccessful()); 46 | } 47 | 48 | @Test 49 | public void testMapReducedJobsWithFailFastSync(){ 50 | // example of ignite jobs, first one succeeded , second fail, third succeeded , but the reducer will fail fast once he collect the failed job 51 | IgniteCallable validationServiceJob1=() -> ServiceResponse.builder().response("Job 1 is valid").serviceOrigin("job1") 52 | .success(true).build(); 53 | IgniteCallable validationServiceJob2=() -> ServiceResponse.builder().response("Job 2 is failed").serviceOrigin("job2") 54 | .success(false).build(); 55 | IgniteCallable validationServiceJob3=() -> ServiceResponse.builder().response("Job 3 is valid").serviceOrigin("job3") 56 | .success(true).build(); 57 | 58 | final MapReduceResponse mapReduceResponse = computeService.validateWithAllServicesInParallelSync( 59 | Arrays.asList(validationServiceJob1,validationServiceJob2,validationServiceJob3) 60 | ); 61 | boolean status=true; 62 | for(ServiceResponse serviceResponse: mapReduceResponse.getReducedResponses().values()){ 63 | 64 | status=status && serviceResponse.isSuccess(); 65 | } 66 | // make sure the aggregated status is failed 67 | assertEquals(status,false); 68 | assertEquals(mapReduceResponse.isSuccess(),false); 69 | 70 | } 71 | 72 | 73 | @Test 74 | public void testMapReducedJobsWithFailFastSyncFirstAllSuccess(){ 75 | // example of ignite jobs, all succeeded , so the reducer collect all and return successfully 76 | IgniteCallable validationServiceJob1=() -> ServiceResponse.builder().serviceOrigin("job1") 77 | .response("Job 1 is valid").success(true).build(); 78 | IgniteCallable validationServiceJob2=() -> ServiceResponse.builder().serviceOrigin("job2") 79 | .response("Job 2 is valid").success(true).build(); 80 | IgniteCallable validationServiceJob3=() -> ServiceResponse.builder().serviceOrigin("job3") 81 | .response("Job 3 is valid").success(true).build(); 82 | final MapReduceResponse mapReduceResponse = computeService.validateWithAllServicesInParallelSync( 83 | Arrays.asList(validationServiceJob1,validationServiceJob2,validationServiceJob3) 84 | ); 85 | boolean status=true; 86 | for(ServiceResponse serviceResponse: mapReduceResponse.getReducedResponses().values()){ 87 | 88 | status=status && serviceResponse.isSuccess(); 89 | } 90 | // make sure the aggregated status is success 91 | assertEquals(status,true); 92 | assertEquals(mapReduceResponse.isSuccess(),true); 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/romeh/ignitemanager/repositories/impl/IgniteAlertsSoreTest.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.repositories.impl; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.mockito.ArgumentMatchers.any; 5 | import static org.mockito.Mockito.when; 6 | 7 | import java.util.Arrays; 8 | 9 | import javax.cache.Cache; 10 | 11 | import org.apache.ignite.Ignite; 12 | import org.apache.ignite.IgniteCache; 13 | import org.apache.ignite.cache.query.QueryCursor; 14 | import org.apache.ignite.cache.query.SqlQuery; 15 | import org.apache.ignite.internal.processors.cache.CacheEntryImpl; 16 | import org.junit.Before; 17 | import org.junit.Test; 18 | import org.junit.runner.RunWith; 19 | import org.mockito.InjectMocks; 20 | import org.mockito.Mock; 21 | import org.mockito.junit.MockitoJUnitRunner; 22 | 23 | import com.romeh.ignitemanager.entities.AlertEntry; 24 | import com.romeh.ignitemanager.entities.CacheNames; 25 | 26 | /** 27 | * Created by romeh on 17/08/2017. 28 | */ 29 | @RunWith(MockitoJUnitRunner.class) 30 | public class IgniteAlertsSoreTest { 31 | @Mock 32 | private Ignite ignite; 33 | @Mock 34 | QueryCursor queryCursor; 35 | @Mock 36 | IgniteCache igniteCache; 37 | @InjectMocks 38 | private IgniteAlertsStore igniteAlertsStore; 39 | 40 | @Before 41 | public void setUp() { 42 | Cache.Entry entry = new CacheEntryImpl("serviceId", AlertEntry.builder().errorCode("errorCode").build()); 43 | when(ignite.cache(CacheNames.Alerts.name())).thenReturn(igniteCache); 44 | when(igniteAlertsStore.getAlertsCache()).thenReturn(igniteCache); 45 | when(igniteCache.query(any(SqlQuery.class))).thenReturn(queryCursor); 46 | when(queryCursor.getAll()).thenReturn(Arrays.asList(entry)); 47 | 48 | } 49 | 50 | @Test 51 | public void getAllAlerts() { 52 | assertEquals(igniteAlertsStore.getAlertForServiceId("serviceId").get(0).getErrorCode(), "errorCode"); 53 | } 54 | 55 | 56 | } -------------------------------------------------------------------------------- /src/test/java/com/romeh/ignitemanager/web/AlertsControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.romeh.ignitemanager.web; 2 | 3 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; 4 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 6 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 8 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 9 | 10 | import java.nio.charset.Charset; 11 | import java.util.ArrayList; 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | import org.junit.Before; 17 | import org.junit.Test; 18 | import org.mockito.Mockito; 19 | import org.springframework.http.MediaType; 20 | import org.springframework.test.util.ReflectionTestUtils; 21 | import org.springframework.test.web.servlet.MockMvc; 22 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 23 | 24 | import com.google.gson.Gson; 25 | import com.romeh.ignitemanager.entities.AlertEntry; 26 | import com.romeh.ignitemanager.services.AlertsService; 27 | 28 | /** 29 | * Created by romeh on 11/08/2017. 30 | */ 31 | public class AlertsControllerTest { 32 | private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), 33 | Charset.forName("utf8")); 34 | 35 | private MockMvc mockMvc; 36 | 37 | private AlertsService alertsService; 38 | 39 | private AlertEntry alertEntry; 40 | 41 | @Before 42 | public void setUp() { 43 | AlertsController alertsController = new AlertsController(); 44 | Map content = new HashMap<>(); 45 | content.put("error", "errorContent"); 46 | alertEntry = AlertEntry.builder().errorCode("errorCode").serviceId("serviceCode").severity("CRITICAL").alertContent(content).build(); 47 | ErrorHandler errorHandler = new ErrorHandler(); 48 | this.alertsService = Mockito.mock(AlertsService.class); 49 | ReflectionTestUtils.setField(alertsController, "alertsService", this.alertsService); 50 | this.mockMvc = MockMvcBuilders.standaloneSetup(alertsController, errorHandler).build(); 51 | 52 | } 53 | 54 | @Test 55 | public void getAllAlerts() throws Exception { 56 | Mockito.when(this.alertsService.getAllAlerts()).thenReturn(new ArrayList<>()); 57 | this.mockMvc.perform(get("/alerts")).andExpect(status().isOk()); 58 | } 59 | 60 | 61 | @Test 62 | public void createAlert() throws Exception { 63 | Mockito.doNothing().when(this.alertsService).createAlertEntry(Mockito.any()); 64 | this.mockMvc.perform(post("/alerts").contentType(APPLICATION_JSON_UTF8).content(new Gson().toJson(alertEntry))).andExpect(status().isCreated()); 65 | } 66 | 67 | 68 | @Test 69 | public void UpdateAlert() throws Exception { 70 | Mockito.doNothing().when(this.alertsService).updateAlertEntry(Mockito.anyString(), Mockito.anyString(), Mockito.any()); 71 | this.mockMvc.perform(put("/alerts/serviceId/codeId").contentType(APPLICATION_JSON_UTF8).content(new Gson().toJson(alertEntry))).andExpect(status().isOk()); 72 | } 73 | 74 | 75 | @Test 76 | public void deleteAlert() throws Exception { 77 | Mockito.doNothing().when(this.alertsService).deleteAlertEntry(Mockito.anyString()); 78 | this.mockMvc.perform(delete("/alerts/serviceId")).andExpect(status().isOk()); 79 | } 80 | 81 | 82 | @Test 83 | public void getServiceAlert() throws Exception { 84 | Mockito.when(this.alertsService.getAlertForServiceId(Mockito.anyString())) 85 | .thenReturn(Arrays.asList(AlertEntry.builder().errorCode("errorCode").serviceId("serviceCode").severity("CRITICAL").build())); 86 | this.mockMvc.perform(get("/alerts/serviceId")).andExpect(status().isOk()).andExpect(content() 87 | .string("[{\"alertContent\":null,\"errorCode\":\"errorCode\",\"serviceId\":\"serviceCode\",\"severity\":\"CRITICAL\",\"timestamp\":null,\"alertId\":null}]")); 88 | } 89 | 90 | 91 | } -------------------------------------------------------------------------------- /src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: romehAlertManager 4 | jackson: 5 | default-property-inclusion: non_null 6 | 7 | mail: 8 | service: 9 | baseUrl: http://romeh-wsitt/Mail/soap/mail 10 | user: romehALERT 11 | password: romehALERT 12 | undeliverable: undeliverable@belgacom-ics.com 13 | from: 14 | bpo.mailbox: romeh.alerts@romeh.com 15 | default: 16 | receiver: mahmoud.romih@romeh.com 17 | 18 | alerts: 19 | alertConfigurations: 20 | - 21 | serviceCode: "IG_TEST" 22 | errorCode: "1001_TEST" 23 | emails: 24 | - "mahmoud.romih@romeh.com.test" 25 | - "mahmoud.romih@romeh.com.test" 26 | maxCount: "3" 27 | mailTemplate: "ticket.html" 28 | - 29 | serviceCode: "IG__TEST" 30 | errorCode: "1002_TEST" 31 | emails: 32 | - "mahmoud.romih@romeh.com.test" 33 | - "mahmoud.romih@romeh.com.test" 34 | maxCount: "2" 35 | mailTemplate: "ticket.html" 36 | 37 | enableFilePersistence: false 38 | igniteConnectorPort: 11211 39 | igniteServerPortRange: 47500..47509 40 | ignitePersistenceFilePath: ./data 41 | initialDelay: 10000 42 | fixedDelay: 30000 -------------------------------------------------------------------------------- /src/test/resources/ignite.xml: -------------------------------------------------------------------------------- 1 | 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 | 127.0.0.1:47500..47509 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 63 | 64 | 65 | 66 | 67 | 68 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 101 | 102 | -------------------------------------------------------------------------------- /src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ${FILE_LOG_PATTERN} 10 | utf8 11 | 12 | 13 | 14 | 15 | ${LOG_HOME}/alertManager.log 16 | true 17 | 18 | ${FILE_LOG_PATTERN} 19 | 20 | 21 | debug.log.%i 22 | 23 | 25 | 10MB 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | --------------------------------------------------------------------------------