forked from exadel-inc/CompreFace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request exadel-inc#213 from exadel-inc/EFRS-835
EFRS-835: synchronize API nodes
- Loading branch information
Showing
21 changed files
with
319 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
api/src/main/java/com/exadel/frs/core/trainservice/config/repository/Listener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.exadel.frs.core.trainservice.config.repository; | ||
|
||
import static com.exadel.frs.core.trainservice.system.global.Constants.SERVER_UUID; | ||
import com.exadel.frs.core.trainservice.dto.DbActionDto; | ||
import com.exadel.frs.core.trainservice.service.DbActionService; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.postgresql.PGConnection; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
@Slf4j | ||
class Listener { | ||
|
||
@Autowired | ||
private DbActionService actionService; | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private final Connection conn; | ||
private final PGConnection pgconn; | ||
|
||
Listener(Connection conn) throws SQLException { | ||
this.conn = conn; | ||
this.pgconn = (PGConnection)conn; | ||
Statement stmt = conn.createStatement(); | ||
stmt.execute("LISTEN face_collection_update_msg"); | ||
stmt.close(); | ||
log.info(String.format("Listener %s is started", SERVER_UUID)); | ||
} | ||
|
||
@Scheduled(fixedRate=500) | ||
public void listen() { | ||
try { | ||
Statement stmt = conn.createStatement(); | ||
ResultSet rs = stmt.executeQuery("SELECT 1"); | ||
rs.close(); | ||
stmt.close(); | ||
|
||
org.postgresql.PGNotification[] notifications = pgconn.getNotifications(); | ||
if (notifications != null) { | ||
for (final org.postgresql.PGNotification notification : notifications) { | ||
log.info("Get notification: " + notification.getName()); | ||
actionService.synchronizeCache(mapper.readValue(notification.getParameter(), DbActionDto.class)); | ||
} | ||
} | ||
} catch (SQLException | JsonProcessingException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
api/src/main/java/com/exadel/frs/core/trainservice/config/repository/NotificationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.exadel.frs.core.trainservice.config.repository; | ||
|
||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class NotificationConfig { | ||
|
||
@Value("${spring.datasource-pg.username}") | ||
private String dbUsername; | ||
|
||
@Value("${spring.datasource-pg.password}") | ||
private String dbPassword; | ||
|
||
@Value("${spring.datasource-pg.url}") | ||
private String dbUrl; | ||
|
||
@Bean | ||
public Listener dbListenerRun() throws SQLException { | ||
return new Listener(DriverManager.getConnection(dbUrl, dbUsername, dbPassword)); | ||
} | ||
|
||
@Bean | ||
public Notifier dbNotifier() throws SQLException { | ||
return new Notifier(DriverManager.getConnection(dbUrl, dbUsername, dbPassword)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
api/src/main/java/com/exadel/frs/core/trainservice/config/repository/Notifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.exadel.frs.core.trainservice.config.repository; | ||
|
||
import com.exadel.frs.core.trainservice.dto.DbActionDto; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public class Notifier{ | ||
|
||
private final Connection conn; | ||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
public Notifier(Connection conn) { | ||
this.conn = conn; | ||
} | ||
|
||
public void notifyWithMessage(DbActionDto actionDto) { | ||
try { | ||
String actionString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(actionDto); | ||
Statement stmt = conn.createStatement(); | ||
stmt.execute(String.format("SELECT pg_notify('face_collection_update_msg', '%s');", actionString)); | ||
stmt.close(); | ||
} catch (SQLException | JsonProcessingException sqle) { | ||
sqle.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
api/src/main/java/com/exadel/frs/core/trainservice/dto/DbActionDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.exadel.frs.core.trainservice.dto; | ||
|
||
import com.exadel.frs.core.trainservice.enums.DbAction; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class DbActionDto { | ||
|
||
@JsonProperty("action") | ||
private DbAction action; | ||
|
||
@JsonProperty("apiKey") | ||
private String apiKey; | ||
|
||
@JsonProperty("faceIds") | ||
private List<String> faceIds; | ||
|
||
@JsonProperty("faceName") | ||
private String faceName; | ||
|
||
@JsonProperty("uuid") | ||
private String serverUUID; | ||
} |
19 changes: 19 additions & 0 deletions
19
api/src/main/java/com/exadel/frs/core/trainservice/enums/DbAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.exadel.frs.core.trainservice.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public enum DbAction { | ||
INSERT("I"), | ||
DELETE("D"), | ||
DELETE_ALL("DA"); | ||
|
||
@Getter | ||
@Setter | ||
private String code; | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
api/src/main/java/com/exadel/frs/core/trainservice/service/DbActionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.exadel.frs.core.trainservice.service; | ||
|
||
import static com.exadel.frs.core.trainservice.system.global.Constants.SERVER_UUID; | ||
import com.exadel.frs.core.trainservice.cache.FaceCacheProvider; | ||
import com.exadel.frs.core.trainservice.dto.DbActionDto; | ||
import com.exadel.frs.core.trainservice.repository.FacesRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DbActionService { | ||
|
||
private final FaceCacheProvider faceCacheProvider; | ||
private final FacesRepository facesRepository; | ||
|
||
public void synchronizeCache(DbActionDto action) { | ||
if (!action.getServerUUID().equals(SERVER_UUID)) { | ||
switch (action.getAction()) { | ||
case DELETE: | ||
action.getFaceIds() | ||
.forEach(face -> faceCacheProvider.getOrLoad(action.getApiKey()) | ||
.removeFace(face, action.getFaceName()) | ||
); | ||
break; | ||
case INSERT: | ||
faceCacheProvider.getOrLoad(action.getApiKey()) | ||
.addFace(facesRepository.findById(action.getFaceIds().get(0)).get()); | ||
break; | ||
case DELETE_ALL: | ||
faceCacheProvider.invalidate(action.getApiKey()); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.