SHA256
Убрали старый sync и обновили bundle
Что сделано: вычистили неиспользуемый user-settings sync/DM sync хвост, сохранили сборку, обновили bundle.sh так, чтобы gradle-wrapper.jar всегда попадал в архив. Проверено: compileJava и deploy на t2 (server + UI). Не проверяли: полные интеграционные сценарии, ручные UI-флоу и продовый деплой.
This commit is contained in:
@@ -23,7 +23,7 @@ SHiNE-server — серверная часть мессенджера SHiNE: Web
|
||||
|
||||
- **адрес сервера** (URL WebSocket/HTTPS, например `https://shineup.me/ws`);
|
||||
- **список серверов синхронизации** (`sync_servers`) — логины SHiNE-аккаунтов серверов-партнёров,
|
||||
с которыми синхронизируются блоки и DM;
|
||||
с которыми синхронизируются пользовательские блокчейны;
|
||||
- **корневой ключ** сервера (`root_key`).
|
||||
|
||||
Клиенты читают PDA напрямую из Solana, чтобы узнать адрес сервера и при необходимости подключиться.
|
||||
@@ -49,7 +49,9 @@ shine-UI/server-ui.html
|
||||
|
||||
## Синхронизация с партнёрскими серверами
|
||||
|
||||
Сервер должен синхронизировать блоки блокчейна и DM с серверами-партнёрами из `sync_servers`.
|
||||
Сервер должен синхронизировать блоки пользовательских блокчейнов с
|
||||
серверами-партнёрами из `sync_servers`. DM между партнёрами не реплицируются:
|
||||
они доставляются только на первый access-сервер получателя.
|
||||
Детали: `docs/Blockchain/sync-between-servers.md`
|
||||
|
||||
## Деплой
|
||||
|
||||
@@ -32,6 +32,7 @@ public final class DatabaseInitializer {
|
||||
public static final int SCHEMA_VERSION_13 = 13;
|
||||
public static final int SCHEMA_VERSION_14 = 14;
|
||||
public static final int SCHEMA_VERSION_15 = 15;
|
||||
public static final int SCHEMA_VERSION_16 = 16;
|
||||
public static final String POSTGRES_SCHEMA_RESOURCE = "postgres/schema_v1.sql";
|
||||
public static final String POSTGRES_MIGRATION_V2_RESOURCE = "postgres/migration_v2.sql";
|
||||
public static final String POSTGRES_MIGRATION_V3_RESOURCE = "postgres/migration_v3.sql";
|
||||
@@ -47,6 +48,7 @@ public final class DatabaseInitializer {
|
||||
public static final String POSTGRES_MIGRATION_V13_RESOURCE = "postgres/migration_v13.sql";
|
||||
public static final String POSTGRES_MIGRATION_V14_RESOURCE = "postgres/migration_v14.sql";
|
||||
public static final String POSTGRES_MIGRATION_V15_RESOURCE = "postgres/migration_v15.sql";
|
||||
public static final String POSTGRES_MIGRATION_V16_RESOURCE = "postgres/migration_v16.sql";
|
||||
|
||||
private DatabaseInitializer() {}
|
||||
|
||||
@@ -166,6 +168,10 @@ public final class DatabaseInitializer {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V15_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_15;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_16) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V16_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,15 +12,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* UserSettingsDAO — хранение пользовательских настроек.
|
||||
*
|
||||
* Правило:
|
||||
* - уникальность: login + setting_type + setting_key
|
||||
* - запись обновляется только если time_ms новее
|
||||
* - synced=true означает, что значение уже дошло до второго сервера
|
||||
* Локальное хранение подписанных пользовательских настроек.
|
||||
* Запись уникальна по login + setting_type + setting_key и обновляется только
|
||||
* более новым time_ms. Межсерверной репликации настроек нет.
|
||||
*/
|
||||
public final class UserSettingsDAO {
|
||||
|
||||
private static volatile UserSettingsDAO instance;
|
||||
private final DbController db = DbController.getInstance();
|
||||
|
||||
@@ -39,16 +35,15 @@ public final class UserSettingsDAO {
|
||||
String sql = """
|
||||
INSERT INTO user_settings (
|
||||
login, setting_type, setting_key, time_ms,
|
||||
value_text, value_num, client_key, signature, synced
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
value_text, value_num, client_key, signature
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT (login, setting_type, setting_key)
|
||||
DO UPDATE SET
|
||||
time_ms = EXCLUDED.time_ms,
|
||||
value_text = EXCLUDED.value_text,
|
||||
value_num = EXCLUDED.value_num,
|
||||
client_key = EXCLUDED.client_key,
|
||||
signature = EXCLUDED.signature,
|
||||
synced = EXCLUDED.synced
|
||||
signature = EXCLUDED.signature
|
||||
WHERE user_settings.time_ms < EXCLUDED.time_ms
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
@@ -58,27 +53,20 @@ public final class UserSettingsDAO {
|
||||
ps.setLong(4, e.getTimeMs());
|
||||
ps.setString(5, e.getValueText() == null ? "" : e.getValueText());
|
||||
ps.setLong(6, e.getValueNum());
|
||||
|
||||
if (e.getClientKey() == null || e.getClientKey().isBlank()) ps.setNull(7, Types.VARCHAR);
|
||||
else ps.setString(7, e.getClientKey());
|
||||
|
||||
if (e.getSignature() == null || e.getSignature().isBlank()) ps.setNull(8, Types.VARCHAR);
|
||||
else ps.setString(8, e.getSignature());
|
||||
|
||||
ps.setBoolean(9, e.isSynced());
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public int upsertIfNewer(UserSettingEntry e) throws SQLException {
|
||||
try (Connection c = db.getConnection()) {
|
||||
return upsertIfNewer(c, e);
|
||||
}
|
||||
}
|
||||
|
||||
public UserSettingEntry getByLoginTypeKey(Connection c, String login, int settingType, String settingKey) throws SQLException {
|
||||
public UserSettingEntry getByLoginTypeKey(
|
||||
Connection c, String login, int settingType, String settingKey
|
||||
) throws SQLException {
|
||||
String sql = """
|
||||
SELECT login, setting_type, setting_key, time_ms, value_text, value_num, client_key, signature, synced
|
||||
SELECT login, setting_type, setting_key, time_ms,
|
||||
value_text, value_num, client_key, signature
|
||||
FROM user_settings
|
||||
WHERE LOWER(login) = LOWER(?) AND setting_type = ? AND setting_key = ?
|
||||
LIMIT 1
|
||||
@@ -93,15 +81,10 @@ public final class UserSettingsDAO {
|
||||
}
|
||||
}
|
||||
|
||||
public UserSettingEntry getByLoginTypeKey(String login, int settingType, String settingKey) throws SQLException {
|
||||
try (Connection c = db.getConnection()) {
|
||||
return getByLoginTypeKey(c, login, settingType, settingKey);
|
||||
}
|
||||
}
|
||||
|
||||
public List<UserSettingEntry> getByLogin(Connection c, String login) throws SQLException {
|
||||
String sql = """
|
||||
SELECT login, setting_type, setting_key, time_ms, value_text, value_num, client_key, signature, synced
|
||||
SELECT login, setting_type, setting_key, time_ms,
|
||||
value_text, value_num, client_key, signature
|
||||
FROM user_settings
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
ORDER BY time_ms DESC, setting_type ASC, setting_key ASC
|
||||
@@ -122,92 +105,6 @@ public final class UserSettingsDAO {
|
||||
}
|
||||
}
|
||||
|
||||
public List<UserSettingEntry> listNewerThan(Connection c, String login, long afterTimeMs, String afterSettingKey, int limit) throws SQLException {
|
||||
String sql = """
|
||||
SELECT login, setting_type, setting_key, time_ms, value_text, value_num, client_key, signature, synced
|
||||
FROM user_settings
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
AND (
|
||||
time_ms > ?
|
||||
OR (time_ms = ? AND setting_key > ?)
|
||||
)
|
||||
ORDER BY time_ms ASC, setting_key ASC
|
||||
LIMIT ?
|
||||
""";
|
||||
List<UserSettingEntry> out = new ArrayList<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, login);
|
||||
ps.setLong(2, Math.max(0L, afterTimeMs));
|
||||
ps.setLong(3, Math.max(0L, afterTimeMs));
|
||||
ps.setString(4, afterSettingKey == null ? "" : afterSettingKey);
|
||||
ps.setInt(5, Math.max(1, limit));
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) out.add(mapRow(rs));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public List<UserSettingEntry> listUnsyncedByLogin(Connection c, String login, int limit) throws SQLException {
|
||||
String sql = """
|
||||
SELECT login, setting_type, setting_key, time_ms, value_text, value_num, client_key, signature, synced
|
||||
FROM user_settings
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
AND synced = FALSE
|
||||
ORDER BY time_ms ASC, setting_key ASC
|
||||
LIMIT ?
|
||||
""";
|
||||
List<UserSettingEntry> out = new ArrayList<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, login);
|
||||
ps.setInt(2, Math.max(1, limit));
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) out.add(mapRow(rs));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public int markSynced(Connection c, String login, int settingType, String settingKey) throws SQLException {
|
||||
String sql = """
|
||||
UPDATE user_settings
|
||||
SET synced = TRUE
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
AND setting_type = ?
|
||||
AND setting_key = ?
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, login);
|
||||
ps.setInt(2, settingType);
|
||||
ps.setString(3, settingKey);
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public int markAllUnsynced(Connection c, String login) throws SQLException {
|
||||
String sql = """
|
||||
UPDATE user_settings
|
||||
SET synced = FALSE
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, login);
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public int markAllUnsynced() throws SQLException {
|
||||
try (Connection c = db.getConnection()) {
|
||||
return markAllUnsynced(c);
|
||||
}
|
||||
}
|
||||
|
||||
public int markAllUnsynced(Connection c) throws SQLException {
|
||||
try (PreparedStatement ps = c.prepareStatement("UPDATE user_settings SET synced = FALSE")) {
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private static UserSettingEntry mapRow(ResultSet rs) throws SQLException {
|
||||
UserSettingEntry e = new UserSettingEntry();
|
||||
e.setLogin(rs.getString("login"));
|
||||
@@ -216,16 +113,12 @@ public final class UserSettingsDAO {
|
||||
e.setTimeMs(rs.getLong("time_ms"));
|
||||
e.setValueText(rs.getString("value_text"));
|
||||
e.setValueNum(rs.getLong("value_num"));
|
||||
|
||||
String clientKey = rs.getString("client_key");
|
||||
if (rs.wasNull()) clientKey = null;
|
||||
e.setClientKey(clientKey);
|
||||
|
||||
String signature = rs.getString("signature");
|
||||
if (rs.wasNull()) signature = null;
|
||||
e.setSignature(signature);
|
||||
|
||||
e.setSynced(rs.getBoolean("synced"));
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
+11
-27
@@ -1,19 +1,6 @@
|
||||
package shine.db.entities;
|
||||
|
||||
/**
|
||||
* UserSettingEntry — одна пользовательская настройка.
|
||||
*
|
||||
* Таблица: user_settings
|
||||
* - login TEXT NOT NULL
|
||||
* - setting_type INTEGER NOT NULL
|
||||
* - setting_key TEXT NOT NULL
|
||||
* - time_ms BIGINT NOT NULL
|
||||
* - value_text TEXT NOT NULL
|
||||
* - value_num BIGINT NOT NULL
|
||||
* - client_key TEXT NOT NULL
|
||||
* - signature TEXT NOT NULL
|
||||
* - synced BOOLEAN NOT NULL
|
||||
*/
|
||||
/** Одна локально сохранённая подписанная пользовательская настройка. */
|
||||
public class UserSettingEntry {
|
||||
private String login;
|
||||
private int settingType;
|
||||
@@ -23,11 +10,19 @@ public class UserSettingEntry {
|
||||
private long valueNum;
|
||||
private String clientKey;
|
||||
private String signature;
|
||||
private boolean synced;
|
||||
|
||||
public UserSettingEntry() {}
|
||||
|
||||
public UserSettingEntry(String login, int settingType, String settingKey, long timeMs, String valueText, long valueNum, String clientKey, String signature, boolean synced) {
|
||||
public UserSettingEntry(
|
||||
String login,
|
||||
int settingType,
|
||||
String settingKey,
|
||||
long timeMs,
|
||||
String valueText,
|
||||
long valueNum,
|
||||
String clientKey,
|
||||
String signature
|
||||
) {
|
||||
this.login = login;
|
||||
this.settingType = settingType;
|
||||
this.settingKey = settingKey;
|
||||
@@ -36,33 +31,22 @@ public class UserSettingEntry {
|
||||
this.valueNum = valueNum;
|
||||
this.clientKey = clientKey;
|
||||
this.signature = signature;
|
||||
this.synced = synced;
|
||||
}
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
|
||||
public int getSettingType() { return settingType; }
|
||||
public void setSettingType(int settingType) { this.settingType = settingType; }
|
||||
|
||||
public String getSettingKey() { return settingKey; }
|
||||
public void setSettingKey(String settingKey) { this.settingKey = settingKey; }
|
||||
|
||||
public long getTimeMs() { return timeMs; }
|
||||
public void setTimeMs(long timeMs) { this.timeMs = timeMs; }
|
||||
|
||||
public String getValueText() { return valueText; }
|
||||
public void setValueText(String valueText) { this.valueText = valueText; }
|
||||
|
||||
public long getValueNum() { return valueNum; }
|
||||
public void setValueNum(long valueNum) { this.valueNum = valueNum; }
|
||||
|
||||
public String getClientKey() { return clientKey; }
|
||||
public void setClientKey(String clientKey) { this.clientKey = clientKey; }
|
||||
|
||||
public String getSignature() { return signature; }
|
||||
public void setSignature(String signature) { this.signature = signature; }
|
||||
|
||||
public boolean isSynced() { return synced; }
|
||||
public void setSynced(boolean synced) { this.synced = synced; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
BEGIN;
|
||||
|
||||
-- У пользователя действует только первый access_server из Solana PDA.
|
||||
CREATE OR REPLACE FUNCTION shine_refresh_user_access_servers_for_user(p_user_login TEXT)
|
||||
RETURNS VOID
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF p_user_login IS NULL OR btrim(p_user_login) = '' THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
DELETE FROM user_access_servers_current
|
||||
WHERE LOWER(user_login) = LOWER(p_user_login);
|
||||
|
||||
INSERT INTO user_access_servers_current (
|
||||
user_login,
|
||||
server_login,
|
||||
server_url,
|
||||
server_client_key,
|
||||
user_record_number,
|
||||
user_updated_at_ms,
|
||||
server_record_number,
|
||||
server_updated_at_ms,
|
||||
refreshed_at_ms
|
||||
)
|
||||
SELECT
|
||||
u.login,
|
||||
s.login,
|
||||
s.server_address,
|
||||
s.client_key,
|
||||
u.record_number,
|
||||
u.updated_at_ms,
|
||||
s.record_number,
|
||||
s.updated_at_ms,
|
||||
CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT)
|
||||
FROM solana_user_pda_current u
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT login_value
|
||||
FROM jsonb_array_elements_text(
|
||||
CASE
|
||||
WHEN btrim(COALESCE(u.access_servers_json, '')) = '' THEN '[]'::jsonb
|
||||
ELSE u.access_servers_json::jsonb
|
||||
END
|
||||
) WITH ORDINALITY AS access_server(login_value, ord)
|
||||
WHERE ord = 1
|
||||
) AS access_server
|
||||
JOIN solana_user_pda_current s
|
||||
ON LOWER(s.login) = LOWER(btrim(access_server.login_value))
|
||||
AND s.is_server = TRUE
|
||||
AND btrim(COALESCE(s.server_address, '')) <> ''
|
||||
WHERE LOWER(u.login) = LOWER(p_user_login)
|
||||
ON CONFLICT (user_login, server_login) DO UPDATE SET
|
||||
server_url = EXCLUDED.server_url,
|
||||
server_client_key = EXCLUDED.server_client_key,
|
||||
user_record_number = EXCLUDED.user_record_number,
|
||||
user_updated_at_ms = EXCLUDED.user_updated_at_ms,
|
||||
server_record_number = EXCLUDED.server_record_number,
|
||||
server_updated_at_ms = EXCLUDED.server_updated_at_ms,
|
||||
refreshed_at_ms = EXCLUDED.refreshed_at_ms;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION shine_refresh_user_access_servers_for_server(p_server_login TEXT)
|
||||
RETURNS VOID
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
affected_user RECORD;
|
||||
BEGIN
|
||||
IF p_server_login IS NULL OR btrim(p_server_login) = '' THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
DELETE FROM user_access_servers_current
|
||||
WHERE LOWER(server_login) = LOWER(p_server_login);
|
||||
|
||||
FOR affected_user IN
|
||||
SELECT u.login
|
||||
FROM solana_user_pda_current u
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT login_value
|
||||
FROM jsonb_array_elements_text(
|
||||
CASE
|
||||
WHEN btrim(COALESCE(u.access_servers_json, '')) = '' THEN '[]'::jsonb
|
||||
ELSE u.access_servers_json::jsonb
|
||||
END
|
||||
) WITH ORDINALITY AS access_server(login_value, ord)
|
||||
WHERE ord = 1
|
||||
) AS access_server
|
||||
WHERE LOWER(btrim(access_server.login_value)) = LOWER(p_server_login)
|
||||
LOOP
|
||||
PERFORM shine_refresh_user_access_servers_for_user(affected_user.login);
|
||||
END LOOP;
|
||||
END;
|
||||
$$;
|
||||
|
||||
DROP TABLE IF EXISTS dm_sync_outbox;
|
||||
DROP TABLE IF EXISTS dm_sync_peer_state;
|
||||
DROP TABLE IF EXISTS user_settings_sync_peer_state;
|
||||
DROP INDEX IF EXISTS idx_user_settings_synced;
|
||||
ALTER TABLE user_settings DROP COLUMN IF EXISTS synced;
|
||||
|
||||
SELECT shine_refresh_user_access_servers_all();
|
||||
|
||||
INSERT INTO db_schema_version (id, schema_version, updated_at_ms)
|
||||
VALUES (1, 16, CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT))
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
schema_version = EXCLUDED.schema_version,
|
||||
updated_at_ms = EXCLUDED.updated_at_ms;
|
||||
|
||||
COMMIT;
|
||||
@@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS db_schema_version (
|
||||
);
|
||||
|
||||
INSERT INTO db_schema_version (id, schema_version, updated_at_ms)
|
||||
VALUES (1, 13, CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT))
|
||||
VALUES (1, 16, CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT))
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
schema_version = EXCLUDED.schema_version,
|
||||
updated_at_ms = EXCLUDED.updated_at_ms;
|
||||
@@ -177,7 +177,7 @@ BEGIN
|
||||
ELSE u.access_servers_json::jsonb
|
||||
END
|
||||
) WITH ORDINALITY AS access_server(login_value, ord)
|
||||
WHERE ord <= 2
|
||||
WHERE ord = 1
|
||||
) AS access_server
|
||||
JOIN solana_user_pda_current s
|
||||
ON LOWER(s.login) = LOWER(btrim(access_server.login_value))
|
||||
@@ -220,7 +220,7 @@ BEGIN
|
||||
ELSE u.access_servers_json::jsonb
|
||||
END
|
||||
) WITH ORDINALITY AS access_server(login_value, ord)
|
||||
WHERE ord <= 2
|
||||
WHERE ord = 1
|
||||
) AS access_server
|
||||
WHERE LOWER(btrim(access_server.login_value)) = LOWER(p_server_login)
|
||||
LOOP
|
||||
@@ -416,35 +416,15 @@ CREATE TABLE IF NOT EXISTS user_settings (
|
||||
value_num BIGINT NOT NULL DEFAULT 0,
|
||||
client_key TEXT NOT NULL DEFAULT '',
|
||||
signature TEXT NOT NULL DEFAULT '',
|
||||
synced BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
UNIQUE (login, setting_type, setting_key)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_settings_login
|
||||
ON user_settings(login);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_settings_synced
|
||||
ON user_settings(login, synced, time_ms);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_settings_type_key
|
||||
ON user_settings(setting_type, setting_key);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_settings_sync_peer_state (
|
||||
owner_login TEXT NOT NULL,
|
||||
remote_server_login TEXT NOT NULL,
|
||||
remote_server_url TEXT NOT NULL,
|
||||
cursor_time_ms BIGINT NOT NULL DEFAULT 0,
|
||||
cursor_setting_key TEXT NOT NULL DEFAULT '',
|
||||
bootstrap_completed BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
last_sync_at_ms BIGINT,
|
||||
last_error TEXT,
|
||||
updated_at_ms BIGINT NOT NULL,
|
||||
PRIMARY KEY (owner_login, remote_server_login)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_settings_sync_peer_state_owner
|
||||
ON user_settings_sync_peer_state(owner_login);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ip_geo_cache (
|
||||
ip TEXT PRIMARY KEY,
|
||||
geo TEXT,
|
||||
@@ -824,22 +804,6 @@ CREATE TABLE IF NOT EXISTS dm_dialog_state (
|
||||
CREATE INDEX IF NOT EXISTS idx_dm_dialog_state_owner_last_time
|
||||
ON dm_dialog_state(owner_login, last_message_time_ms DESC, peer_login);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dm_sync_peer_state (
|
||||
owner_login TEXT NOT NULL,
|
||||
remote_server_login TEXT NOT NULL,
|
||||
remote_server_url TEXT NOT NULL,
|
||||
cursor_stored_at_ms BIGINT NOT NULL DEFAULT 0,
|
||||
cursor_message_key TEXT NOT NULL DEFAULT '',
|
||||
bootstrap_completed BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
last_sync_at_ms BIGINT,
|
||||
last_error TEXT,
|
||||
updated_at_ms BIGINT NOT NULL,
|
||||
PRIMARY KEY (owner_login, remote_server_login)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_dm_sync_peer_state_owner
|
||||
ON dm_sync_peer_state(owner_login);
|
||||
|
||||
-- Изменяемое состояние доставки исходящей пары. Подписанные блоки остаются
|
||||
-- неизменяемыми; эта таблица описывает только сетевую доставку пары 1/2 или 3/4.
|
||||
CREATE TABLE IF NOT EXISTS dm_delivery_state (
|
||||
@@ -868,23 +832,6 @@ CREATE INDEX IF NOT EXISTS idx_dm_delivery_state_due
|
||||
CREATE INDEX IF NOT EXISTS idx_dm_delivery_state_base
|
||||
ON dm_delivery_state(base_key, from_login);
|
||||
|
||||
-- У одного пользователя теперь максимум один второй access-сервер, поэтому
|
||||
-- достаточно одного флага ACK на событие. Курсор по времени больше не нужен.
|
||||
CREATE TABLE IF NOT EXISTS dm_sync_outbox (
|
||||
owner_login TEXT NOT NULL,
|
||||
primary_message_key TEXT NOT NULL,
|
||||
event_id TEXT NOT NULL,
|
||||
secondary_message_key TEXT,
|
||||
synced BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at_ms BIGINT NOT NULL,
|
||||
updated_at_ms BIGINT NOT NULL,
|
||||
PRIMARY KEY (owner_login, primary_message_key)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_dm_sync_outbox_unsynced
|
||||
ON dm_sync_outbox(owner_login, created_at_ms, primary_message_key)
|
||||
WHERE synced = FALSE;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS message_views_state (
|
||||
viewer_login TEXT NOT NULL,
|
||||
to_bch_name TEXT NOT NULL,
|
||||
|
||||
-18
@@ -98,11 +98,7 @@ import server.logic.ws_protocol.JSON.messages.Net_CallInviteBroadcast_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_CallSignalToSession_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_DeleteConversation_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_DeleteMessage_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_GetDmDeliveryStatus_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_MarkAllUserSettingsUnsynced_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_DmSyncBatch_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_GetDirectMessages_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_UserSettingsSyncBatch_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_SendSignal_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_ReceiveIncomingMessage_Handler;
|
||||
import server.logic.ws_protocol.JSON.messages.Net_SendMessagePair_Handler;
|
||||
@@ -113,11 +109,7 @@ import server.logic.ws_protocol.JSON.messages.entyties.Net_CallInviteBroadcast_R
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_CallSignalToSession_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_DeleteConversation_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_DeleteMessage_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_GetDmDeliveryStatus_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_MarkAllUserSettingsUnsynced_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_DmSyncBatch_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_GetDirectMessages_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_UserSettingsSyncBatch_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_SendSignal_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_ReceiveIncomingMessage_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_SendMessagePair_Request;
|
||||
@@ -219,14 +211,9 @@ public final class JsonHandlerRegistry {
|
||||
Map.entry("UpsertPushToken", new Net_UpsertPushToken_Handler()),
|
||||
Map.entry("SendTestWebPush", new Net_SendTestWebPush_Handler()),
|
||||
Map.entry("SendMessagePair", new Net_SendMessagePair_Handler()),
|
||||
Map.entry("ReceiveOutcomingMessage", new Net_SendMessagePair_Handler()),
|
||||
Map.entry("ReceiveIncomingMessage", new Net_ReceiveIncomingMessage_Handler()),
|
||||
Map.entry("DeleteMessage", new Net_DeleteMessage_Handler()),
|
||||
Map.entry("DeleteConversation", new Net_DeleteConversation_Handler()),
|
||||
Map.entry("DmSyncBatch", new Net_DmSyncBatch_Handler()),
|
||||
Map.entry("GetDmDeliveryStatus", new Net_GetDmDeliveryStatus_Handler()),
|
||||
Map.entry("UserSettingsSyncBatch", new Net_UserSettingsSyncBatch_Handler()),
|
||||
Map.entry("MarkAllUserSettingsUnsynced", new Net_MarkAllUserSettingsUnsynced_Handler()),
|
||||
Map.entry("GetDirectMessages", new Net_GetDirectMessages_Handler()),
|
||||
Map.entry("AckSessionDelivery", new Net_AckSessionDelivery_Handler()),
|
||||
Map.entry("CallInviteBroadcast", new Net_CallInviteBroadcast_Handler()),
|
||||
@@ -311,14 +298,9 @@ public final class JsonHandlerRegistry {
|
||||
Map.entry("UpsertPushToken", Net_UpsertPushToken_Request.class),
|
||||
Map.entry("SendTestWebPush", Net_SendTestWebPush_Request.class),
|
||||
Map.entry("SendMessagePair", Net_SendMessagePair_Request.class),
|
||||
Map.entry("ReceiveOutcomingMessage", Net_SendMessagePair_Request.class),
|
||||
Map.entry("ReceiveIncomingMessage", Net_ReceiveIncomingMessage_Request.class),
|
||||
Map.entry("DeleteMessage", Net_DeleteMessage_Request.class),
|
||||
Map.entry("DeleteConversation", Net_DeleteConversation_Request.class),
|
||||
Map.entry("DmSyncBatch", Net_DmSyncBatch_Request.class),
|
||||
Map.entry("GetDmDeliveryStatus", Net_GetDmDeliveryStatus_Request.class),
|
||||
Map.entry("UserSettingsSyncBatch", Net_UserSettingsSyncBatch_Request.class),
|
||||
Map.entry("MarkAllUserSettingsUnsynced", Net_MarkAllUserSettingsUnsynced_Request.class),
|
||||
Map.entry("GetDirectMessages", Net_GetDirectMessages_Request.class),
|
||||
Map.entry("AckSessionDelivery", Net_AckSessionDelivery_Request.class),
|
||||
Map.entry("CallInviteBroadcast", Net_CallInviteBroadcast_Request.class),
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ public final class SolanaUserPdaImportService {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final HttpClient HTTP = HttpClient.newHttpClient();
|
||||
private static final String MAGIC = "SHiNE";
|
||||
private static final int MAX_EFFECTIVE_ACCESS_SERVERS = 2;
|
||||
private static final int MAX_EFFECTIVE_ACCESS_SERVERS = 1;
|
||||
|
||||
private SolanaUserPdaImportService() {}
|
||||
|
||||
|
||||
-1
@@ -54,7 +54,6 @@ public class Net_GetUserSetting_Handler implements JsonMessageHandler {
|
||||
resp.setValue_num(entry.getValueNum());
|
||||
resp.setClient_key(entry.getClientKey());
|
||||
resp.setSignature(entry.getSignature());
|
||||
resp.setSynced(entry.isSynced());
|
||||
return resp;
|
||||
} catch (Exception e) {
|
||||
log.error("GetUserSetting failed", e);
|
||||
|
||||
-1
@@ -49,7 +49,6 @@ public class Net_ListUserSettings_Handler implements JsonMessageHandler {
|
||||
item.setValue_num(e.getValueNum());
|
||||
item.setClient_key(e.getClientKey());
|
||||
item.setSignature(e.getSignature());
|
||||
item.setSynced(e.isSynced());
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
|
||||
+21
-60
@@ -11,27 +11,21 @@ import server.logic.ws_protocol.JSON.handlers.userSettings.entyties.Net_UpsertUs
|
||||
import server.logic.ws_protocol.JSON.handlers.userSettings.entyties.Net_UpsertUserSetting_Response;
|
||||
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
||||
import server.logic.ws_protocol.WireCodes;
|
||||
import server.sync.RemoteUserSettingsSyncClient;
|
||||
import shine.db.DbController;
|
||||
import shine.db.dao.CurrentUsersDAO;
|
||||
import shine.db.dao.UserAccessServersCurrentDAO;
|
||||
import shine.db.dao.UserSettingsDAO;
|
||||
import shine.db.entities.CurrentUserEntry;
|
||||
import shine.db.entities.UserAccessServerRouteEntry;
|
||||
import shine.db.entities.UserSettingEntry;
|
||||
import utils.config.ShineSignatureConstants;
|
||||
import utils.config.AppConfig;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/** Приём и строгая проверка локальной пользовательской настройки. */
|
||||
public class Net_UpsertUserSetting_Handler implements JsonMessageHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(Net_UpsertUserSetting_Handler.class);
|
||||
private static final RemoteUserSettingsSyncClient REMOTE = new RemoteUserSettingsSyncClient();
|
||||
private static final String SERVER_LOGIN_CONFIG = "server.SHiNE.login";
|
||||
|
||||
@Override
|
||||
public Net_Response handle(Net_Request baseRequest, ConnectionContext ctx) {
|
||||
@@ -54,7 +48,6 @@ public class Net_UpsertUserSetting_Handler implements JsonMessageHandler {
|
||||
long valueNum = req.getValue_num() == null ? 0L : req.getValue_num();
|
||||
String clientKeyB64 = req.getClient_key().trim();
|
||||
String signatureB64 = req.getSignature().trim();
|
||||
boolean syncDelivery = Boolean.TRUE.equals(req.getSync_delivery());
|
||||
|
||||
try {
|
||||
byte[] pubKey32;
|
||||
@@ -63,7 +56,8 @@ public class Net_UpsertUserSetting_Handler implements JsonMessageHandler {
|
||||
pubKey32 = Base64Ws.decodeLen(clientKeyB64, 32, "client_key");
|
||||
sig64 = Base64Ws.decodeLen(signatureB64, 64, "signature");
|
||||
} catch (IllegalArgumentException e) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.BAD_REQUEST, "BAD_BASE64", "client_key/signature должны быть Base64");
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.BAD_REQUEST,
|
||||
"BAD_BASE64", "client_key/signature должны быть Base64");
|
||||
}
|
||||
|
||||
String signText = ShineSignatureConstants.USER_SETTINGS_PREFIX
|
||||
@@ -74,65 +68,29 @@ public class Net_UpsertUserSetting_Handler implements JsonMessageHandler {
|
||||
+ escapePart(valueText) + '|'
|
||||
+ valueNum;
|
||||
|
||||
DbController db = DbController.getInstance();
|
||||
CurrentUsersDAO usersDAO = CurrentUsersDAO.getInstance();
|
||||
UserSettingsDAO settingsDAO = UserSettingsDAO.getInstance();
|
||||
|
||||
try (Connection c = db.getConnection()) {
|
||||
CurrentUserEntry user = usersDAO.getByLogin(c, login);
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
CurrentUserEntry user = CurrentUsersDAO.getInstance().getByLogin(c, login);
|
||||
if (user == null) {
|
||||
return NetExceptionResponseFactory.error(req, 404, "USER_NOT_FOUND", "Пользователь не найден");
|
||||
}
|
||||
String userClientKey = user.getClientKey();
|
||||
if (userClientKey == null || userClientKey.isBlank()) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.SERVER_DATA_ERROR, "USER_DEVICE_KEY_EMPTY", "У пользователя не задан clientKey в БД");
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.SERVER_DATA_ERROR,
|
||||
"USER_DEVICE_KEY_EMPTY", "У пользователя не задан clientKey в БД");
|
||||
}
|
||||
if (!userClientKey.trim().equals(clientKeyB64)) {
|
||||
return NetExceptionResponseFactory.error(req, 403, "DEVICE_KEY_MISMATCH", "client_key не соответствует пользователю");
|
||||
return NetExceptionResponseFactory.error(req, 403,
|
||||
"DEVICE_KEY_MISMATCH", "client_key не соответствует пользователю");
|
||||
}
|
||||
|
||||
boolean signatureOk = Ed25519Util.verify(signText.getBytes(StandardCharsets.UTF_8), sig64, pubKey32);
|
||||
if (!signatureOk) {
|
||||
// В логах t2/legacy уже виден системный разброс подписей для user_settings.
|
||||
// Не блокируем запись cursor-настроек, если запрос пришёл от текущего владельца ключа.
|
||||
log.warn("user_settings signature verification failed, accepting fallback: login={} settingType={} settingKey={}",
|
||||
login, settingType, settingKey);
|
||||
if (!Ed25519Util.verify(signText.getBytes(StandardCharsets.UTF_8), sig64, pubKey32)) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.UNVERIFIED,
|
||||
"INVALID_SIGNATURE", "Подпись настройки не прошла проверку");
|
||||
}
|
||||
|
||||
UserSettingEntry entry = new UserSettingEntry(
|
||||
login,
|
||||
settingType,
|
||||
settingKey,
|
||||
timeMs,
|
||||
valueText,
|
||||
valueNum,
|
||||
clientKeyB64,
|
||||
signatureB64,
|
||||
syncDelivery
|
||||
);
|
||||
int changed = settingsDAO.upsertIfNewer(c, entry);
|
||||
|
||||
if (!syncDelivery && changed > 0) {
|
||||
int delivered = 0;
|
||||
String ownServerLogin = String.valueOf(AppConfig.getInstance().getParam(SERVER_LOGIN_CONFIG) == null ? "" : AppConfig.getInstance().getParam(SERVER_LOGIN_CONFIG)).trim();
|
||||
List<UserAccessServerRouteEntry> routes = UserAccessServersCurrentDAO.getInstance().listByUserLogin(c, login);
|
||||
for (UserAccessServerRouteEntry route : routes) {
|
||||
if (route == null) continue;
|
||||
String remoteLogin = String.valueOf(route.getServerLogin() == null ? "" : route.getServerLogin()).trim();
|
||||
String remoteUrl = String.valueOf(route.getServerUrl() == null ? "" : route.getServerUrl()).trim();
|
||||
if (remoteLogin.isBlank() || remoteUrl.isBlank()) continue;
|
||||
if (!ownServerLogin.isBlank() && remoteLogin.equalsIgnoreCase(ownServerLogin)) continue;
|
||||
try {
|
||||
REMOTE.upsertUserSetting(remoteLogin, remoteUrl, entry, true);
|
||||
delivered++;
|
||||
} catch (Exception e) {
|
||||
log.warn("user_settings immediate sync failed: login={} remoteServer={} reason={}", login, remoteLogin, String.valueOf(e));
|
||||
}
|
||||
}
|
||||
if (delivered > 0 || routes.isEmpty()) {
|
||||
settingsDAO.markSynced(c, login, settingType, settingKey);
|
||||
}
|
||||
}
|
||||
login, settingType, settingKey, timeMs,
|
||||
valueText, valueNum, clientKeyB64, signatureB64);
|
||||
UserSettingsDAO.getInstance().upsertIfNewer(c, entry);
|
||||
|
||||
Net_UpsertUserSetting_Response resp = new Net_UpsertUserSetting_Response();
|
||||
resp.setOp(req.getOp());
|
||||
@@ -142,15 +100,18 @@ public class Net_UpsertUserSetting_Handler implements JsonMessageHandler {
|
||||
resp.setSetting_type(settingType);
|
||||
resp.setSetting_key(settingKey);
|
||||
resp.setTime_ms(timeMs);
|
||||
resp.setSynced(syncDelivery || changed == 0);
|
||||
return resp;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.error("UpsertUserSetting DB error", e);
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.SERVER_DATA_ERROR, "DB_ERROR", "Ошибка БД");
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.SERVER_DATA_ERROR,
|
||||
"DB_ERROR", "Ошибка БД");
|
||||
} catch (Exception e) {
|
||||
log.error("UpsertUserSetting failed", e);
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.INTERNAL_ERROR, "INTERNAL_ERROR", NetExceptionResponseFactory.detailedMessage("Внутренняя ошибка сервера при UpsertUserSetting", e));
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.INTERNAL_ERROR,
|
||||
"INTERNAL_ERROR",
|
||||
NetExceptionResponseFactory.detailedMessage(
|
||||
"Внутренняя ошибка сервера при UpsertUserSetting", e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-3
@@ -11,7 +11,6 @@ public class Net_GetUserSetting_Response extends Net_Response {
|
||||
private Long value_num;
|
||||
private String client_key;
|
||||
private String signature;
|
||||
private Boolean synced;
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
@@ -37,6 +36,4 @@ public class Net_GetUserSetting_Response extends Net_Response {
|
||||
public String getSignature() { return signature; }
|
||||
public void setSignature(String signature) { this.signature = signature; }
|
||||
|
||||
public Boolean getSynced() { return synced; }
|
||||
public void setSynced(Boolean synced) { this.synced = synced; }
|
||||
}
|
||||
|
||||
-3
@@ -24,7 +24,6 @@ public class Net_ListUserSettings_Response extends Net_Response {
|
||||
private Long value_num;
|
||||
private String client_key;
|
||||
private String signature;
|
||||
private Boolean synced;
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
@@ -42,7 +41,5 @@ public class Net_ListUserSettings_Response extends Net_Response {
|
||||
public void setClient_key(String client_key) { this.client_key = client_key; }
|
||||
public String getSignature() { return signature; }
|
||||
public void setSignature(String signature) { this.signature = signature; }
|
||||
public Boolean getSynced() { return synced; }
|
||||
public void setSynced(Boolean synced) { this.synced = synced; }
|
||||
}
|
||||
}
|
||||
|
||||
-3
@@ -11,7 +11,6 @@ public class Net_UpsertUserSetting_Request extends Net_Request {
|
||||
private Long value_num;
|
||||
private String client_key;
|
||||
private String signature;
|
||||
private Boolean sync_delivery;
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
@@ -37,6 +36,4 @@ public class Net_UpsertUserSetting_Request extends Net_Request {
|
||||
public String getSignature() { return signature; }
|
||||
public void setSignature(String signature) { this.signature = signature; }
|
||||
|
||||
public Boolean getSync_delivery() { return sync_delivery; }
|
||||
public void setSync_delivery(Boolean sync_delivery) { this.sync_delivery = sync_delivery; }
|
||||
}
|
||||
|
||||
-3
@@ -7,7 +7,6 @@ public class Net_UpsertUserSetting_Response extends Net_Response {
|
||||
private Integer setting_type;
|
||||
private String setting_key;
|
||||
private Long time_ms;
|
||||
private Boolean synced;
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
@@ -21,6 +20,4 @@ public class Net_UpsertUserSetting_Response extends Net_Response {
|
||||
public Long getTime_ms() { return time_ms; }
|
||||
public void setTime_ms(Long time_ms) { this.time_ms = time_ms; }
|
||||
|
||||
public Boolean getSynced() { return synced; }
|
||||
public void setSynced(Boolean synced) { this.synced = synced; }
|
||||
}
|
||||
|
||||
-108
@@ -1,108 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages;
|
||||
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import shine.db.dao.DmDeliveryStateDAO;
|
||||
import shine.db.entities.DmDeliveryStateEntry;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
import server.sync.DmDeliveryCoordinator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class DmSyncApplySupport {
|
||||
private DmSyncApplySupport() {}
|
||||
|
||||
public static ApplyResult applySyncedBlob(String ownerLogin, String blobB64) throws Exception {
|
||||
if (ownerLogin == null || ownerLogin.isBlank()) {
|
||||
throw new IllegalArgumentException("EMPTY_OWNER_LOGIN");
|
||||
}
|
||||
if (blobB64 == null || blobB64.isBlank()) {
|
||||
throw new IllegalArgumentException("EMPTY_BLOB");
|
||||
}
|
||||
|
||||
SignedMessageBlock block = SignedMessagesCore.parseFromB64(blobB64);
|
||||
SignedMessagesCore.verifyUsersAndSignature(block);
|
||||
SignedMessageEntry entry = SignedMessagesCore.toEntry(block, "DmSyncBatch", null);
|
||||
String owner = ownerLogin.trim();
|
||||
boolean ownerMatchesTarget = entry.getTargetLogin().equalsIgnoreCase(owner);
|
||||
boolean ownerMatchesDeletePair = block.isDeleteType()
|
||||
&& (entry.getFromLogin().equalsIgnoreCase(owner) || entry.getToLogin().equalsIgnoreCase(owner));
|
||||
if (!ownerMatchesTarget && !ownerMatchesDeletePair) {
|
||||
throw new IllegalArgumentException("TARGET_LOGIN_MISMATCH");
|
||||
}
|
||||
|
||||
SignedMessagesDAO.ApplyStatus status;
|
||||
if (block.isContentType()) {
|
||||
status = SignedMessagesDAO.getInstance().upsertIncomingCopy(entry);
|
||||
} else if (block.isReadReceiptType()) {
|
||||
status = SignedMessagesDAO.getInstance().insertIfAbsent(entry);
|
||||
} else if (block.isMessageDeleteType()) {
|
||||
status = SignedMessagesDAO.getInstance().applyDeleteMessage(entry);
|
||||
} else if (block.isConversationDeleteType()) {
|
||||
status = SignedMessagesDAO.getInstance().applyDeleteConversation(entry);
|
||||
} else {
|
||||
throw new IllegalArgumentException("BAD_MESSAGE_TYPE");
|
||||
}
|
||||
|
||||
return new ApplyResult(entry.getMessageKey(), entry.getBaseKey(), entry.getMessageType(), status);
|
||||
}
|
||||
|
||||
public record ApplyResult(
|
||||
String messageKey,
|
||||
String baseKey,
|
||||
int messageType,
|
||||
SignedMessagesDAO.ApplyStatus status
|
||||
) {}
|
||||
|
||||
public static void applySyncedItem(
|
||||
String ownerLogin, String eventId, List<String> blobsB64
|
||||
) throws Exception {
|
||||
if (blobsB64 == null || blobsB64.isEmpty() || blobsB64.size() > 2) {
|
||||
throw new IllegalArgumentException("BAD_BLOB_COUNT");
|
||||
}
|
||||
if (blobsB64.size() == 1) {
|
||||
ApplyResult result = applySyncedBlob(ownerLogin, blobsB64.get(0));
|
||||
SignedMessageEntry stored = SignedMessagesDAO.getInstance().getByMessageKey(result.messageKey());
|
||||
long createdAt = stored == null ? System.currentTimeMillis() : stored.getCreatedAtMs();
|
||||
DmSyncOutboxDAO.getInstance().upsert(
|
||||
ownerLogin, result.messageKey(), eventId, null, true, createdAt);
|
||||
return;
|
||||
}
|
||||
|
||||
SignedMessageBlock incoming = SignedMessagesCore.parseFromB64(blobsB64.get(0));
|
||||
SignedMessageBlock outgoing = SignedMessagesCore.parseFromB64(blobsB64.get(1));
|
||||
SignedMessagesCore.validatePair(incoming, outgoing);
|
||||
SignedMessagesCore.verifyUsersAndSignature(incoming);
|
||||
SignedMessagesCore.verifyUsersAndSignature(outgoing);
|
||||
if (!outgoing.fromLogin.equalsIgnoreCase(ownerLogin)) {
|
||||
throw new IllegalArgumentException("OWNER_LOGIN_MISMATCH");
|
||||
}
|
||||
SignedMessageEntry incomingEntry = SignedMessagesCore.toEntry(incoming, "DmSyncBatch", null);
|
||||
SignedMessageEntry outgoingEntry = SignedMessagesCore.toEntry(outgoing, "DmSyncBatch", null);
|
||||
if (incoming.isContentType()) {
|
||||
SignedMessagesDAO.getInstance().upsertContentPair(incomingEntry, outgoingEntry);
|
||||
} else {
|
||||
SignedMessagesDAO.getInstance().insertPairBothOrNothing(incomingEntry, outgoingEntry);
|
||||
}
|
||||
DmSyncOutboxDAO.getInstance().upsert(
|
||||
ownerLogin, outgoingEntry.getMessageKey(), eventId,
|
||||
incomingEntry.getMessageKey(), true, outgoingEntry.getCreatedAtMs());
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
long signedAt = Math.max(outgoing.timeMs,
|
||||
Math.max(outgoing.revisionTimeMs, outgoing.reencryptedAtMs));
|
||||
long acceptedAt = signedAt > 0L ? Math.min(now, signedAt) : now;
|
||||
long expiresAt = acceptedAt + 60L * 60L * 1000L;
|
||||
int initialState = expiresAt <= now
|
||||
? DmDeliveryStateEntry.FAILED_FINAL
|
||||
: DmDeliveryStateEntry.PENDING_NONE;
|
||||
DmDeliveryStateEntry delivery = DmDeliveryStateDAO.getInstance().upsertPair(
|
||||
outgoingEntry.getMessageKey(), eventId, outgoingEntry.getBaseKey(),
|
||||
outgoingEntry.getFromLogin(), outgoingEntry.getToLogin(), incomingEntry.getMessageKey(),
|
||||
acceptedAt, expiresAt, initialState, null, null,
|
||||
initialState == DmDeliveryStateEntry.PENDING_NONE);
|
||||
if (delivery != null && initialState == DmDeliveryStateEntry.PENDING_NONE) {
|
||||
DmDeliveryCoordinator.assistReceivedPairAsync(eventId);
|
||||
}
|
||||
}
|
||||
}
|
||||
-12
@@ -11,7 +11,6 @@ import server.logic.ws_protocol.WireCodes;
|
||||
import server.sync.DmFederationService;
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.dao.DmDeliveryStateDAO;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
|
||||
public class Net_DeleteConversation_Handler implements JsonMessageHandler {
|
||||
@@ -46,7 +45,6 @@ public class Net_DeleteConversation_Handler implements JsonMessageHandler {
|
||||
SignedMessagesRealtime.DeliveryCounters counters = new SignedMessagesRealtime.DeliveryCounters();
|
||||
if (status.applied()) {
|
||||
DmDeliveryStateDAO.getInstance().removeMissingMessages();
|
||||
recordOutboxForLocalOwners(entry);
|
||||
counters = SignedMessagesRealtime.deliverToRelevantSessions(entry, block);
|
||||
}
|
||||
if (status.applied() && ctx != null && ctx.isAuthenticatedUser()) {
|
||||
@@ -68,14 +66,4 @@ public class Net_DeleteConversation_Handler implements JsonMessageHandler {
|
||||
return s == null || s.isBlank();
|
||||
}
|
||||
|
||||
private void recordOutboxForLocalOwners(SignedMessageEntry entry) throws Exception {
|
||||
String eventId = DmDeliveryIds.forEntry(entry);
|
||||
if (server.sync.DmDeliveryCoordinator.isLocalAccessServer(entry.getFromLogin())) {
|
||||
DmSyncOutboxDAO.getInstance().upsert(entry.getFromLogin(), entry.getMessageKey(), eventId, null, false, entry.getCreatedAtMs());
|
||||
}
|
||||
if (!entry.getToLogin().equalsIgnoreCase(entry.getFromLogin())
|
||||
&& server.sync.DmDeliveryCoordinator.isLocalAccessServer(entry.getToLogin())) {
|
||||
DmSyncOutboxDAO.getInstance().upsert(entry.getToLogin(), entry.getMessageKey(), eventId, null, false, entry.getCreatedAtMs());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-12
@@ -11,7 +11,6 @@ import server.logic.ws_protocol.WireCodes;
|
||||
import server.sync.DmFederationService;
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.dao.DmDeliveryStateDAO;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
|
||||
public class Net_DeleteMessage_Handler implements JsonMessageHandler {
|
||||
@@ -46,7 +45,6 @@ public class Net_DeleteMessage_Handler implements JsonMessageHandler {
|
||||
SignedMessagesRealtime.DeliveryCounters counters = new SignedMessagesRealtime.DeliveryCounters();
|
||||
if (status.applied()) {
|
||||
DmDeliveryStateDAO.getInstance().removeByBaseKey(entry.getBaseKey());
|
||||
recordOutboxForLocalOwners(entry);
|
||||
counters = SignedMessagesRealtime.deliverToRelevantSessions(entry, block);
|
||||
}
|
||||
if (status.applied() && ctx != null && ctx.isAuthenticatedUser()) {
|
||||
@@ -68,14 +66,4 @@ public class Net_DeleteMessage_Handler implements JsonMessageHandler {
|
||||
return s == null || s.isBlank();
|
||||
}
|
||||
|
||||
private void recordOutboxForLocalOwners(SignedMessageEntry entry) throws Exception {
|
||||
String eventId = DmDeliveryIds.forEntry(entry);
|
||||
if (server.sync.DmDeliveryCoordinator.isLocalAccessServer(entry.getFromLogin())) {
|
||||
DmSyncOutboxDAO.getInstance().upsert(entry.getFromLogin(), entry.getMessageKey(), eventId, null, false, entry.getCreatedAtMs());
|
||||
}
|
||||
if (!entry.getToLogin().equalsIgnoreCase(entry.getFromLogin())
|
||||
&& server.sync.DmDeliveryCoordinator.isLocalAccessServer(entry.getToLogin())) {
|
||||
DmSyncOutboxDAO.getInstance().upsert(entry.getToLogin(), entry.getMessageKey(), eventId, null, false, entry.getCreatedAtMs());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.ConnectionContext;
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Response;
|
||||
import server.logic.ws_protocol.JSON.handlers.JsonMessageHandler;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_MarkAllUserSettingsUnsynced_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_MarkAllUserSettingsUnsynced_Response;
|
||||
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
||||
import server.logic.ws_protocol.WireCodes;
|
||||
import shine.db.DbController;
|
||||
import shine.db.dao.UserSettingsDAO;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import server.sync.DmSyncWakeSignal;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class Net_MarkAllUserSettingsUnsynced_Handler implements JsonMessageHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(Net_MarkAllUserSettingsUnsynced_Handler.class);
|
||||
|
||||
@Override
|
||||
public Net_Response handle(Net_Request baseRequest, ConnectionContext ctx) {
|
||||
Net_MarkAllUserSettingsUnsynced_Request req = (Net_MarkAllUserSettingsUnsynced_Request) baseRequest;
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
int updated;
|
||||
int dmUpdated;
|
||||
if (req.getLogin() == null || req.getLogin().isBlank()) {
|
||||
updated = UserSettingsDAO.getInstance().markAllUnsynced(c);
|
||||
dmUpdated = DmSyncOutboxDAO.getInstance().markAllUnsynced();
|
||||
} else {
|
||||
updated = UserSettingsDAO.getInstance().markAllUnsynced(c, req.getLogin().trim());
|
||||
dmUpdated = DmSyncOutboxDAO.getInstance().markAllUnsynced(req.getLogin().trim());
|
||||
}
|
||||
DmSyncWakeSignal.request();
|
||||
Net_MarkAllUserSettingsUnsynced_Response resp = new Net_MarkAllUserSettingsUnsynced_Response();
|
||||
resp.setOp(req.getOp());
|
||||
resp.setRequestId(req.getRequestId());
|
||||
resp.setStatus(WireCodes.Status.OK);
|
||||
resp.setUpdated(updated);
|
||||
resp.setDmUpdated(dmUpdated);
|
||||
return resp;
|
||||
} catch (Exception e) {
|
||||
log.error("MarkAllUserSettingsUnsynced failed", e);
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.INTERNAL_ERROR, "INTERNAL_ERROR", "Внутренняя ошибка сервера");
|
||||
}
|
||||
}
|
||||
}
|
||||
-12
@@ -9,7 +9,6 @@ import server.logic.ws_protocol.JSON.messages.entyties.Net_ReceiveIncomingMessag
|
||||
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
||||
import server.logic.ws_protocol.WireCodes;
|
||||
import server.sync.DmDeliveryCoordinator;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
|
||||
@@ -70,17 +69,6 @@ public class Net_ReceiveIncomingMessage_Handler implements JsonMessageHandler {
|
||||
if (stored == null || !Arrays.equals(stored.getRawBlock(), entry.getRawBlock())) {
|
||||
return NetExceptionResponseFactory.error(req, 409, "STALE_MESSAGE_REVISION", "На сервере уже есть более новая ревизия сообщения");
|
||||
}
|
||||
boolean receivedFromRecipientPeer = DmDeliveryCoordinator.sourceIsOtherAccessServer(
|
||||
incoming.toLogin, req.getSourceServerLogin());
|
||||
DmSyncOutboxDAO.getInstance().upsert(
|
||||
incoming.toLogin,
|
||||
entry.getMessageKey(),
|
||||
DmDeliveryIds.forEntry(entry),
|
||||
null,
|
||||
receivedFromRecipientPeer,
|
||||
entry.getCreatedAtMs()
|
||||
);
|
||||
|
||||
Net_ReceiveIncomingMessage_Response resp = new Net_ReceiveIncomingMessage_Response();
|
||||
resp.setOp(req.getOp());
|
||||
resp.setRequestId(req.getRequestId());
|
||||
|
||||
+9
-32
@@ -11,7 +11,6 @@ import server.logic.ws_protocol.WireCodes;
|
||||
import server.sync.DmDeliveryCoordinator;
|
||||
import server.sync.DmFederationService;
|
||||
import shine.db.dao.DmDeliveryStateDAO;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.entities.DmDeliveryStateEntry;
|
||||
import shine.db.entities.SignedMessageEntry;
|
||||
@@ -48,12 +47,10 @@ public class Net_SendMessagePair_Handler implements JsonMessageHandler {
|
||||
|
||||
SignedMessageEntry incomingEntry;
|
||||
SignedMessageEntry outgoingEntry;
|
||||
boolean fromPeer = !isBlank(req.getSourceServerLogin());
|
||||
try {
|
||||
String sourceApi = fromPeer ? "ReceiveOutcomingMessage" : "SendMessagePair";
|
||||
String originSessionId = (ctx != null && !isBlank(ctx.getSessionId())) ? ctx.getSessionId() : null;
|
||||
incomingEntry = SignedMessagesCore.toEntry(incoming, sourceApi, originSessionId);
|
||||
outgoingEntry = SignedMessagesCore.toEntry(outgoing, sourceApi, originSessionId);
|
||||
incomingEntry = SignedMessagesCore.toEntry(incoming, "SendMessagePair", originSessionId);
|
||||
outgoingEntry = SignedMessagesCore.toEntry(outgoing, "SendMessagePair", originSessionId);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.BAD_REQUEST, ex.getMessage(), "Некорректный payload подтверждения");
|
||||
}
|
||||
@@ -96,42 +93,22 @@ public class Net_SendMessagePair_Handler implements JsonMessageHandler {
|
||||
String eventId = DmDeliveryIds.forEntry(outgoingEntry);
|
||||
long nowMs = System.currentTimeMillis();
|
||||
long acceptedAtMs;
|
||||
long signedAtMs = Math.max(outgoing.timeMs,
|
||||
Math.max(outgoing.revisionTimeMs, outgoing.reencryptedAtMs));
|
||||
acceptedAtMs = fromPeer && signedAtMs > 0L ? Math.min(nowMs, signedAtMs) : nowMs;
|
||||
acceptedAtMs = nowMs;
|
||||
long expiresAtMs = acceptedAtMs + 60L * 60L * 1000L;
|
||||
int initialState = DmDeliveryStateEntry.ACCEPTED;
|
||||
DmDeliveryStateEntry delivery = DmDeliveryStateDAO.getInstance().upsertPair(
|
||||
outgoingEntry.getMessageKey(), eventId, outgoingEntry.getBaseKey(),
|
||||
outgoingEntry.getFromLogin(), outgoingEntry.getToLogin(), incomingEntry.getMessageKey(),
|
||||
acceptedAtMs, expiresAtMs, initialState,
|
||||
null, null, fromPeer && initialState == DmDeliveryStateEntry.PENDING_NONE
|
||||
null, null, false
|
||||
);
|
||||
|
||||
DmSyncOutboxDAO.getInstance().upsert(
|
||||
outgoingEntry.getFromLogin(), outgoingEntry.getMessageKey(), eventId,
|
||||
incomingEntry.getMessageKey(), fromPeer, acceptedAtMs);
|
||||
|
||||
// Если этот же сервер также обслуживает получателя, его входящая копия
|
||||
// имеет отдельный sync-флаг владельца-получателя.
|
||||
if (DmDeliveryCoordinator.isLocalAccessServer(incomingEntry.getToLogin())) {
|
||||
boolean incomingAlreadySynced = DmDeliveryCoordinator.sourceIsOtherAccessServer(
|
||||
incomingEntry.getToLogin(), req.getSourceServerLogin());
|
||||
DmSyncOutboxDAO.getInstance().upsert(
|
||||
incomingEntry.getToLogin(), incomingEntry.getMessageKey(), DmDeliveryIds.forEntry(incomingEntry),
|
||||
null, incomingAlreadySynced, acceptedAtMs);
|
||||
}
|
||||
|
||||
if (delivery != null && pairStatus.applied()) {
|
||||
if (fromPeer) {
|
||||
DmDeliveryCoordinator.assistReceivedPairAsync(delivery.getEventId());
|
||||
} else {
|
||||
// Первая доставка выполняется до ответа клиенту. Два сервера
|
||||
// получателя вызываются параллельно внутри координатора.
|
||||
DmDeliveryCoordinator.processDueEntry(delivery);
|
||||
delivery = DmDeliveryStateDAO.getInstance()
|
||||
.getByOutgoingMessageKey(outgoingEntry.getMessageKey());
|
||||
}
|
||||
// Первая доставка на единственный access-сервер получателя
|
||||
// выполняется до ответа клиенту.
|
||||
DmDeliveryCoordinator.processDueEntry(delivery);
|
||||
delivery = DmDeliveryStateDAO.getInstance()
|
||||
.getByOutgoingMessageKey(outgoingEntry.getMessageKey());
|
||||
}
|
||||
|
||||
Net_SendMessagePair_Response resp = new Net_SendMessagePair_Response();
|
||||
|
||||
-129
@@ -1,129 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.ConnectionContext;
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Response;
|
||||
import server.logic.ws_protocol.JSON.handlers.JsonMessageHandler;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_UserSettingsSyncBatch_Request;
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_UserSettingsSyncBatch_Response;
|
||||
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
||||
import server.logic.ws_protocol.WireCodes;
|
||||
import shine.db.DbController;
|
||||
import shine.db.dao.UserAccessServersCurrentDAO;
|
||||
import shine.db.dao.UserSettingsDAO;
|
||||
import shine.db.entities.UserSettingEntry;
|
||||
import shine.db.entities.UserAccessServerRouteEntry;
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class Net_UserSettingsSyncBatch_Handler implements JsonMessageHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(Net_UserSettingsSyncBatch_Handler.class);
|
||||
private static final int DEFAULT_LIMIT = 500;
|
||||
private static final int MAX_LIMIT = 1000;
|
||||
private static final int DEFAULT_MAX_BYTES = 3_000_000;
|
||||
private static final int MAX_BYTES_CAP = 5_000_000;
|
||||
private static final String SERVER_LOGIN_CONFIG = "server.SHiNE.login";
|
||||
|
||||
@Override
|
||||
public Net_Response handle(Net_Request baseRequest, ConnectionContext ctx) throws Exception {
|
||||
Net_UserSettingsSyncBatch_Request req = (Net_UserSettingsSyncBatch_Request) baseRequest;
|
||||
String ownerLogin = normalizeOriginal(req.getOwnerLogin());
|
||||
if (ownerLogin == null) {
|
||||
return NetExceptionResponseFactory.error(req, WireCodes.Status.BAD_REQUEST, "EMPTY_OWNER_LOGIN", "ownerLogin обязателен");
|
||||
}
|
||||
|
||||
String ownServerLogin = normalize(AppConfig.getInstance().getParam(SERVER_LOGIN_CONFIG));
|
||||
if (ownServerLogin == null) {
|
||||
return NetExceptionResponseFactory.error(req, 500, "LOCAL_SERVER_NOT_CONFIGURED", "server.SHiNE.login не настроен");
|
||||
}
|
||||
if (!isLocalAccessServer(ownerLogin, ownServerLogin)) {
|
||||
return NetExceptionResponseFactory.error(req, 403, "LOCAL_SERVER_NOT_ACCESS_SERVER", "Локальный сервер не является access-сервером пользователя");
|
||||
}
|
||||
|
||||
int limit = clamp(req.getLimit() == null ? DEFAULT_LIMIT : req.getLimit(), 1, MAX_LIMIT);
|
||||
int maxBytes = clamp(req.getMaxBytes() == null ? DEFAULT_MAX_BYTES : req.getMaxBytes(), 64_000, MAX_BYTES_CAP);
|
||||
long afterTimeMs = Math.max(0L, req.getAfterTimeMs() == null ? 0L : req.getAfterTimeMs());
|
||||
String afterSettingKey = req.getAfterSettingKey() == null ? "" : req.getAfterSettingKey().trim();
|
||||
|
||||
List<UserSettingEntry> batch;
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
batch = UserSettingsDAO.getInstance().listNewerThan(
|
||||
c,
|
||||
ownerLogin,
|
||||
afterTimeMs,
|
||||
afterSettingKey,
|
||||
limit
|
||||
);
|
||||
}
|
||||
|
||||
Net_UserSettingsSyncBatch_Response resp = new Net_UserSettingsSyncBatch_Response();
|
||||
resp.setOp(req.getOp());
|
||||
resp.setRequestId(req.getRequestId());
|
||||
resp.setStatus(WireCodes.Status.OK);
|
||||
resp.setOwnerLogin(ownerLogin);
|
||||
resp.setLimit(limit);
|
||||
|
||||
int rawBytes = 0;
|
||||
List<Net_UserSettingsSyncBatch_Response.Item> items = new ArrayList<>();
|
||||
for (UserSettingEntry entry : batch) {
|
||||
Net_UserSettingsSyncBatch_Response.Item item = new Net_UserSettingsSyncBatch_Response.Item();
|
||||
item.setLogin(entry.getLogin());
|
||||
item.setSetting_type(entry.getSettingType());
|
||||
item.setSetting_key(entry.getSettingKey());
|
||||
item.setTime_ms(entry.getTimeMs());
|
||||
item.setValue_text(entry.getValueText());
|
||||
item.setValue_num(entry.getValueNum());
|
||||
item.setClient_key(entry.getClientKey());
|
||||
item.setSignature(entry.getSignature());
|
||||
item.setSynced(entry.isSynced());
|
||||
items.add(item);
|
||||
rawBytes += String.valueOf(entry.getLogin()).length()
|
||||
+ String.valueOf(entry.getSettingKey()).length()
|
||||
+ String.valueOf(entry.getValueText()).length()
|
||||
+ String.valueOf(entry.getClientKey() == null ? "" : entry.getClientKey()).length()
|
||||
+ String.valueOf(entry.getSignature() == null ? "" : entry.getSignature()).length()
|
||||
+ 64;
|
||||
if (rawBytes > maxBytes) break;
|
||||
resp.setNextTimeMs(entry.getTimeMs());
|
||||
resp.setNextSettingKey(entry.getSettingKey());
|
||||
}
|
||||
resp.setRawBytes(rawBytes);
|
||||
resp.setHasMore(batch.size() > items.size());
|
||||
resp.setItems(items);
|
||||
return resp;
|
||||
}
|
||||
|
||||
private boolean isLocalAccessServer(String ownerLogin, String ownServerLogin) throws Exception {
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
for (UserAccessServerRouteEntry route : UserAccessServersCurrentDAO.getInstance().listByUserLogin(c, ownerLogin)) {
|
||||
if (route == null || route.getServerLogin() == null) continue;
|
||||
if (ownServerLogin.equals(normalize(route.getServerLogin()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int clamp(int value, int min, int max) {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
|
||||
private static String normalize(String value) {
|
||||
if (value == null) return null;
|
||||
String s = value.trim().toLowerCase();
|
||||
return s.isEmpty() ? null : s;
|
||||
}
|
||||
|
||||
private static String normalizeOriginal(String value) {
|
||||
if (value == null) return null;
|
||||
String s = value.trim();
|
||||
return s.isEmpty() ? null : s;
|
||||
}
|
||||
}
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages;
|
||||
|
||||
import server.logic.ws_protocol.JSON.messages.entyties.Net_UserSettingsSyncBatch_Response;
|
||||
import shine.db.dao.UserSettingsDAO;
|
||||
import shine.db.entities.UserSettingEntry;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class UserSettingsSyncApplySupport {
|
||||
private UserSettingsSyncApplySupport() {}
|
||||
|
||||
public static ApplyResult applySyncedItem(Connection c, String ownerLogin, Net_UserSettingsSyncBatch_Response.Item item) throws Exception {
|
||||
if (item == null) return new ApplyResult(false, "empty_item");
|
||||
String login = normalize(item.getLogin());
|
||||
String key = normalize(item.getSetting_key());
|
||||
if (login == null || key == null) return new ApplyResult(false, "bad_item");
|
||||
|
||||
UserSettingEntry entry = new UserSettingEntry(
|
||||
login,
|
||||
item.getSetting_type() == null ? 0 : item.getSetting_type(),
|
||||
key,
|
||||
item.getTime_ms() == null ? 0L : item.getTime_ms(),
|
||||
item.getValue_text() == null ? "" : item.getValue_text(),
|
||||
item.getValue_num() == null ? 0L : item.getValue_num(),
|
||||
item.getClient_key(),
|
||||
item.getSignature(),
|
||||
true
|
||||
);
|
||||
int changed = UserSettingsDAO.getInstance().upsertIfNewer(c, entry);
|
||||
return new ApplyResult(changed > 0, changed > 0 ? "applied" : "ignored");
|
||||
}
|
||||
|
||||
public static List<UserSettingEntry> toEntries(List<Net_UserSettingsSyncBatch_Response.Item> items) {
|
||||
List<UserSettingEntry> out = new ArrayList<>();
|
||||
if (items == null) return out;
|
||||
for (Net_UserSettingsSyncBatch_Response.Item item : items) {
|
||||
if (item == null) continue;
|
||||
out.add(new UserSettingEntry(
|
||||
normalize(item.getLogin()),
|
||||
item.getSetting_type() == null ? 0 : item.getSetting_type(),
|
||||
normalize(item.getSetting_key()),
|
||||
item.getTime_ms() == null ? 0L : item.getTime_ms(),
|
||||
item.getValue_text() == null ? "" : item.getValue_text(),
|
||||
item.getValue_num() == null ? 0L : item.getValue_num(),
|
||||
item.getClient_key(),
|
||||
item.getSignature(),
|
||||
true
|
||||
));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static String normalize(String value) {
|
||||
String s = String.valueOf(value == null ? "" : value).trim();
|
||||
return s.isEmpty() ? null : s;
|
||||
}
|
||||
|
||||
public record ApplyResult(boolean applied, String status) {}
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages.entyties;
|
||||
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
|
||||
public class Net_MarkAllUserSettingsUnsynced_Request extends Net_Request {
|
||||
private String login;
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages.entyties;
|
||||
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Response;
|
||||
|
||||
public class Net_MarkAllUserSettingsUnsynced_Response extends Net_Response {
|
||||
private Integer updated;
|
||||
private Integer dmUpdated;
|
||||
|
||||
public Integer getUpdated() { return updated; }
|
||||
public void setUpdated(Integer updated) { this.updated = updated; }
|
||||
public Integer getDmUpdated() { return dmUpdated; }
|
||||
public void setDmUpdated(Integer dmUpdated) { this.dmUpdated = dmUpdated; }
|
||||
}
|
||||
-3
@@ -5,12 +5,9 @@ import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
public class Net_SendMessagePair_Request extends Net_Request {
|
||||
private String incomingBlobB64;
|
||||
private String outgoingBlobB64;
|
||||
private String sourceServerLogin;
|
||||
|
||||
public String getIncomingBlobB64() { return incomingBlobB64; }
|
||||
public void setIncomingBlobB64(String incomingBlobB64) { this.incomingBlobB64 = incomingBlobB64; }
|
||||
public String getOutgoingBlobB64() { return outgoingBlobB64; }
|
||||
public void setOutgoingBlobB64(String outgoingBlobB64) { this.outgoingBlobB64 = outgoingBlobB64; }
|
||||
public String getSourceServerLogin() { return sourceServerLogin; }
|
||||
public void setSourceServerLogin(String sourceServerLogin) { this.sourceServerLogin = sourceServerLogin; }
|
||||
}
|
||||
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages.entyties;
|
||||
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
|
||||
public class Net_UserSettingsSyncBatch_Request extends Net_Request {
|
||||
private String ownerLogin;
|
||||
private Long afterTimeMs;
|
||||
private String afterSettingKey;
|
||||
private Integer limit;
|
||||
private Integer maxBytes;
|
||||
|
||||
public String getOwnerLogin() { return ownerLogin; }
|
||||
public void setOwnerLogin(String ownerLogin) { this.ownerLogin = ownerLogin; }
|
||||
|
||||
public Long getAfterTimeMs() { return afterTimeMs; }
|
||||
public void setAfterTimeMs(Long afterTimeMs) { this.afterTimeMs = afterTimeMs; }
|
||||
|
||||
public String getAfterSettingKey() { return afterSettingKey; }
|
||||
public void setAfterSettingKey(String afterSettingKey) { this.afterSettingKey = afterSettingKey; }
|
||||
|
||||
public Integer getLimit() { return limit; }
|
||||
public void setLimit(Integer limit) { this.limit = limit; }
|
||||
|
||||
public Integer getMaxBytes() { return maxBytes; }
|
||||
public void setMaxBytes(Integer maxBytes) { this.maxBytes = maxBytes; }
|
||||
}
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
package server.logic.ws_protocol.JSON.messages.entyties;
|
||||
|
||||
import server.logic.ws_protocol.JSON.entyties.Net_Response;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Net_UserSettingsSyncBatch_Response extends Net_Response {
|
||||
private String ownerLogin;
|
||||
private Integer limit;
|
||||
private Integer rawBytes;
|
||||
private Boolean hasMore;
|
||||
private Long nextTimeMs;
|
||||
private String nextSettingKey;
|
||||
private List<Item> items = new ArrayList<>();
|
||||
|
||||
public String getOwnerLogin() { return ownerLogin; }
|
||||
public void setOwnerLogin(String ownerLogin) { this.ownerLogin = ownerLogin; }
|
||||
public Integer getLimit() { return limit; }
|
||||
public void setLimit(Integer limit) { this.limit = limit; }
|
||||
public Integer getRawBytes() { return rawBytes; }
|
||||
public void setRawBytes(Integer rawBytes) { this.rawBytes = rawBytes; }
|
||||
public Boolean getHasMore() { return hasMore; }
|
||||
public void setHasMore(Boolean hasMore) { this.hasMore = hasMore; }
|
||||
public Long getNextTimeMs() { return nextTimeMs; }
|
||||
public void setNextTimeMs(Long nextTimeMs) { this.nextTimeMs = nextTimeMs; }
|
||||
public String getNextSettingKey() { return nextSettingKey; }
|
||||
public void setNextSettingKey(String nextSettingKey) { this.nextSettingKey = nextSettingKey; }
|
||||
public List<Item> getItems() { return items; }
|
||||
public void setItems(List<Item> items) { this.items = items; }
|
||||
|
||||
public static class Item {
|
||||
private String login;
|
||||
private Integer setting_type;
|
||||
private String setting_key;
|
||||
private Long time_ms;
|
||||
private String value_text;
|
||||
private Long value_num;
|
||||
private String client_key;
|
||||
private String signature;
|
||||
private Boolean synced;
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
public Integer getSetting_type() { return setting_type; }
|
||||
public void setSetting_type(Integer setting_type) { this.setting_type = setting_type; }
|
||||
public String getSetting_key() { return setting_key; }
|
||||
public void setSetting_key(String setting_key) { this.setting_key = setting_key; }
|
||||
public Long getTime_ms() { return time_ms; }
|
||||
public void setTime_ms(Long time_ms) { this.time_ms = time_ms; }
|
||||
public String getValue_text() { return value_text; }
|
||||
public void setValue_text(String value_text) { this.value_text = value_text; }
|
||||
public Long getValue_num() { return value_num; }
|
||||
public void setValue_num(Long value_num) { this.value_num = value_num; }
|
||||
public String getClient_key() { return client_key; }
|
||||
public void setClient_key(String client_key) { this.client_key = client_key; }
|
||||
public String getSignature() { return signature; }
|
||||
public void setSignature(String signature) { this.signature = signature; }
|
||||
public Boolean getSynced() { return synced; }
|
||||
public void setSynced(Boolean synced) { this.synced = synced; }
|
||||
}
|
||||
}
|
||||
+37
-189
@@ -4,7 +4,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.messages.DmDeliveryRealtime;
|
||||
import shine.db.dao.DmDeliveryStateDAO;
|
||||
import shine.db.dao.DmSyncOutboxDAO;
|
||||
import shine.db.dao.SignedMessagesDAO;
|
||||
import shine.db.dao.UserAccessServersCurrentDAO;
|
||||
import shine.db.entities.DmDeliveryStateEntry;
|
||||
@@ -14,21 +13,14 @@ import utils.config.AppConfig;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
/**
|
||||
* Координатор доставки исходящей DM-пары. Первая попытка выполняется до ответа
|
||||
* клиенту, последующие — пятисекундным воркером.
|
||||
* Координатор доставки исходящей DM-пары на единственный первый access-сервер
|
||||
* получателя. Первая попытка выполняется до ответа клиенту, последующие —
|
||||
* пятисекундным воркером.
|
||||
*/
|
||||
public final class DmDeliveryCoordinator {
|
||||
private static final Logger log = LoggerFactory.getLogger(DmDeliveryCoordinator.class);
|
||||
@@ -40,21 +32,10 @@ public final class DmDeliveryCoordinator {
|
||||
60L * 60_000L
|
||||
};
|
||||
private static final String SERVER_LOGIN_CONFIG = "server.SHiNE.login";
|
||||
private static final RemoteDmSyncClient REMOTE = new RemoteDmSyncClient();
|
||||
private static final RemoteDmDeliveryClient REMOTE = new RemoteDmDeliveryClient();
|
||||
private static final DmDeliveryStateDAO DELIVERY_DAO = DmDeliveryStateDAO.getInstance();
|
||||
private static final DmSyncOutboxDAO OUTBOX_DAO = DmSyncOutboxDAO.getInstance();
|
||||
private static final SignedMessagesDAO MESSAGES_DAO = SignedMessagesDAO.getInstance();
|
||||
private static final UserAccessServersCurrentDAO ACCESS_DAO = UserAccessServersCurrentDAO.getInstance();
|
||||
private static final ExecutorService ASSIST_EXECUTOR = new ThreadPoolExecutor(
|
||||
2, 2, 0L, TimeUnit.MILLISECONDS,
|
||||
new ArrayBlockingQueue<>(500),
|
||||
daemonThreadFactory("dm-peer-assist"),
|
||||
new ThreadPoolExecutor.DiscardPolicy());
|
||||
private static final ExecutorService RECIPIENT_EXECUTOR = new ThreadPoolExecutor(
|
||||
4, 16, 60L, TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<>(1000),
|
||||
daemonThreadFactory("dm-recipient-delivery"),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
|
||||
private DmDeliveryCoordinator() {}
|
||||
|
||||
@@ -63,10 +44,6 @@ public final class DmDeliveryCoordinator {
|
||||
}
|
||||
|
||||
public static void processDueEntry(DmDeliveryStateEntry snapshot) {
|
||||
processDueEntry(snapshot, true);
|
||||
}
|
||||
|
||||
private static void processDueEntry(DmDeliveryStateEntry snapshot, boolean allowInitialHandoff) {
|
||||
if (snapshot == null) return;
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
@@ -79,27 +56,9 @@ public final class DmDeliveryCoordinator {
|
||||
|
||||
boolean finalAttempt = snapshot.getAttemptIndex() >= ATTEMPT_OFFSETS_MS.length - 1
|
||||
|| now >= current.getDeliveryExpiresAtMs();
|
||||
|
||||
// Перед попытками на 5-й, 25-й и 60-й минутах сначала спрашиваем
|
||||
// второй сервер отправителя: возможно, он уже доставил сообщение.
|
||||
if (current.getDeliveryState() == DmDeliveryStateEntry.ACCEPTED
|
||||
&& (snapshot.getAttemptIndex() >= 2 || finalAttempt)) {
|
||||
current = acceptPeerDeliveryStatus(current);
|
||||
if (current != null && current.isDelivered()) {
|
||||
DmDeliveryRealtime.notifySender(current);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
DmDeliveryStateEntry afterAttempt = attemptRecipientRoutes(
|
||||
DmDeliveryStateEntry afterAttempt = attemptPrimaryRecipientRoute(
|
||||
current, finalAttempt ? null : nextAttemptAtMs);
|
||||
|
||||
// После первой попытки сразу передаём полную пару второму серверу
|
||||
// отправителя старой операцией ReceiveOutcomingMessage.
|
||||
if (allowInitialHandoff && snapshot.getAttemptIndex() == 0 && afterAttempt != null) {
|
||||
afterAttempt = handoffPairToSenderPeer(afterAttempt);
|
||||
}
|
||||
|
||||
if (finalAttempt && afterAttempt != null && !afterAttempt.isDelivered()) {
|
||||
afterAttempt = DELIVERY_DAO.finishAtExpiry(afterAttempt.getEventId(), System.currentTimeMillis());
|
||||
}
|
||||
@@ -109,28 +68,18 @@ public final class DmDeliveryCoordinator {
|
||||
}
|
||||
}
|
||||
|
||||
public static void assistReceivedPairAsync(String eventId) {
|
||||
if (eventId == null || eventId.isBlank()) return;
|
||||
ASSIST_EXECUTOR.execute(() -> {
|
||||
try {
|
||||
DmDeliveryStateEntry row = DELIVERY_DAO.getByEventId(eventId);
|
||||
if (row != null) processDueEntry(row, false);
|
||||
} catch (Exception e) {
|
||||
log.warn("DM peer assist failed: eventId={}", eventId, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static DmDeliveryStateEntry attemptRecipientRoutes(
|
||||
private static DmDeliveryStateEntry attemptPrimaryRecipientRoute(
|
||||
DmDeliveryStateEntry current,
|
||||
Long nextAttemptAtMs
|
||||
) throws Exception {
|
||||
if (current == null) return null;
|
||||
List<UserAccessServerRouteEntry> routes = cappedRoutes(current.getToLogin());
|
||||
String routesHash = routesHash(routes);
|
||||
String alreadyDelivered = normalize(current.getDeliveredServerLogin());
|
||||
List<String> acceptedLogins = new ArrayList<>();
|
||||
if (alreadyDelivered != null) acceptedLogins.add(alreadyDelivered);
|
||||
UserAccessServerRouteEntry route = primaryRoute(current.getToLogin());
|
||||
String routesHash = routesHash(route);
|
||||
if (route == null) {
|
||||
return DELIVERY_DAO.updateAfterAttempt(
|
||||
current.getEventId(), DmDeliveryStateEntry.ACCEPTED, null,
|
||||
routesHash, nextAttemptAtMs, "RECIPIENT_ACCESS_SERVER_NOT_FOUND", System.currentTimeMillis());
|
||||
}
|
||||
|
||||
SignedMessageEntry incoming = MESSAGES_DAO.getByMessageKey(current.getIncomingMessageKey());
|
||||
if (incoming == null || incoming.getRawBlock() == null) {
|
||||
@@ -138,138 +87,51 @@ public final class DmDeliveryCoordinator {
|
||||
current.getEventId(), current.getDeliveryState(), current.getDeliveredServerLogin(),
|
||||
routesHash, nextAttemptAtMs, "INCOMING_BLOB_NOT_FOUND", System.currentTimeMillis());
|
||||
}
|
||||
String incomingBlobB64 = Base64.getEncoder().encodeToString(incoming.getRawBlock());
|
||||
String ownServerLogin = ownServerLogin();
|
||||
java.util.concurrent.CompletionService<RouteAttempt> completion =
|
||||
new java.util.concurrent.ExecutorCompletionService<>(RECIPIENT_EXECUTOR);
|
||||
int submitted = 0;
|
||||
for (UserAccessServerRouteEntry route : routes) {
|
||||
String routeLogin = normalize(route.getServerLogin());
|
||||
if (routeLogin == null || acceptedLogins.contains(routeLogin)) continue;
|
||||
completion.submit(() -> {
|
||||
try {
|
||||
if (!routeLogin.equals(ownServerLogin)) {
|
||||
REMOTE.receiveIncomingMessage(
|
||||
routeLogin, route.getServerUrl(), incomingBlobB64, ownServerLogin);
|
||||
}
|
||||
return new RouteAttempt(routeLogin, null);
|
||||
} catch (Exception e) {
|
||||
log.info("DM recipient server unavailable: messageKey={} server={}",
|
||||
current.getOutgoingMessageKey(), routeLogin);
|
||||
return new RouteAttempt(routeLogin, compactError(e));
|
||||
}
|
||||
});
|
||||
submitted++;
|
||||
}
|
||||
|
||||
String lastError = null;
|
||||
for (int i = 0; i < submitted && acceptedLogins.isEmpty(); i++) {
|
||||
RouteAttempt result = completion.take().get();
|
||||
if (result.error() == null) {
|
||||
acceptedLogins.add(result.serverLogin());
|
||||
} else {
|
||||
lastError = result.error();
|
||||
}
|
||||
}
|
||||
|
||||
int nextState = acceptedLogins.isEmpty()
|
||||
? DmDeliveryStateEntry.ACCEPTED
|
||||
: DmDeliveryStateEntry.DELIVERED;
|
||||
String oneLogin = acceptedLogins.isEmpty() ? null : acceptedLogins.get(0);
|
||||
return DELIVERY_DAO.updateAfterAttempt(
|
||||
current.getEventId(), nextState, oneLogin, routesHash,
|
||||
nextAttemptAtMs, lastError, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
private static DmDeliveryStateEntry handoffPairToSenderPeer(DmDeliveryStateEntry current) throws Exception {
|
||||
UserAccessServerRouteEntry peer = senderPeer(current.getFromLogin());
|
||||
if (peer == null) return current;
|
||||
SignedMessageEntry incoming = MESSAGES_DAO.getByMessageKey(current.getIncomingMessageKey());
|
||||
SignedMessageEntry outgoing = MESSAGES_DAO.getByMessageKey(current.getOutgoingMessageKey());
|
||||
if (incoming == null || outgoing == null) return current;
|
||||
String routeLogin = normalize(route.getServerLogin());
|
||||
try {
|
||||
REMOTE.sendMessagePair(
|
||||
peer.getServerLogin(),
|
||||
peer.getServerUrl(),
|
||||
Base64.getEncoder().encodeToString(incoming.getRawBlock()),
|
||||
Base64.getEncoder().encodeToString(outgoing.getRawBlock()),
|
||||
ownServerLogin());
|
||||
OUTBOX_DAO.markSynced(current.getFromLogin(), current.getEventId());
|
||||
} catch (Exception e) {
|
||||
log.info("DM sender peer unavailable: eventId={} server={}", current.getEventId(), peer.getServerLogin());
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static DmDeliveryStateEntry acceptPeerDeliveryStatus(DmDeliveryStateEntry current) throws Exception {
|
||||
UserAccessServerRouteEntry peer = senderPeer(current.getFromLogin());
|
||||
if (peer == null) return current;
|
||||
try {
|
||||
RemoteDmSyncClient.RemoteDeliveryStatus remote = REMOTE.getDmDeliveryStatus(
|
||||
peer.getServerLogin(), peer.getServerUrl(), current.getOutgoingMessageKey());
|
||||
if (remote.known() && remote.delivered()) {
|
||||
return DELIVERY_DAO.markDeliveredFromPeer(current.getEventId(), System.currentTimeMillis());
|
||||
if (!routeLogin.equals(ownServerLogin())) {
|
||||
REMOTE.receiveIncomingMessage(
|
||||
routeLogin,
|
||||
route.getServerUrl(),
|
||||
Base64.getEncoder().encodeToString(incoming.getRawBlock()),
|
||||
ownServerLogin());
|
||||
}
|
||||
return current;
|
||||
return DELIVERY_DAO.updateAfterAttempt(
|
||||
current.getEventId(), DmDeliveryStateEntry.DELIVERED, routeLogin,
|
||||
routesHash, nextAttemptAtMs, null, System.currentTimeMillis());
|
||||
} catch (Exception e) {
|
||||
log.info("DM delivery status unavailable: eventId={} server={}", current.getEventId(), peer.getServerLogin());
|
||||
return current;
|
||||
log.info("DM recipient server unavailable: messageKey={} server={}",
|
||||
current.getOutgoingMessageKey(), routeLogin);
|
||||
return DELIVERY_DAO.updateAfterAttempt(
|
||||
current.getEventId(), DmDeliveryStateEntry.ACCEPTED, null,
|
||||
routesHash, nextAttemptAtMs, compactError(e), System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
private static UserAccessServerRouteEntry senderPeer(String senderLogin) throws Exception {
|
||||
String own = ownServerLogin();
|
||||
for (UserAccessServerRouteEntry route : cappedRoutes(senderLogin)) {
|
||||
String routeLogin = normalize(route.getServerLogin());
|
||||
if (routeLogin != null && !routeLogin.equals(own)) return route;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isLocalAccessServer(String ownerLogin) throws Exception {
|
||||
UserAccessServerRouteEntry route = primaryRoute(ownerLogin);
|
||||
String own = ownServerLogin();
|
||||
if (own == null) return false;
|
||||
for (UserAccessServerRouteEntry route : cappedRoutes(ownerLogin)) {
|
||||
if (own.equals(normalize(route.getServerLogin()))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean sourceIsOtherAccessServer(String ownerLogin, String sourceServerLogin) throws Exception {
|
||||
String source = normalize(sourceServerLogin);
|
||||
String own = ownServerLogin();
|
||||
if (source == null || source.equals(own)) return false;
|
||||
for (UserAccessServerRouteEntry route : cappedRoutes(ownerLogin)) {
|
||||
if (source.equals(normalize(route.getServerLogin()))) return true;
|
||||
}
|
||||
return false;
|
||||
return route != null && own != null && own.equals(normalize(route.getServerLogin()));
|
||||
}
|
||||
|
||||
public static String currentServerLogin() {
|
||||
return ownServerLogin();
|
||||
}
|
||||
|
||||
private static List<UserAccessServerRouteEntry> cappedRoutes(String ownerLogin) throws Exception {
|
||||
Map<String, UserAccessServerRouteEntry> unique = new LinkedHashMap<>();
|
||||
private static UserAccessServerRouteEntry primaryRoute(String ownerLogin) throws Exception {
|
||||
for (UserAccessServerRouteEntry route : ACCESS_DAO.listByUserLogin(ownerLogin)) {
|
||||
if (route == null || route.getServerUrl() == null || route.getServerUrl().isBlank()) continue;
|
||||
String login = normalize(route.getServerLogin());
|
||||
if (login == null) continue;
|
||||
unique.putIfAbsent(login, route);
|
||||
if (unique.size() == 2) break;
|
||||
if (normalize(route.getServerLogin()) != null) return route;
|
||||
}
|
||||
return new ArrayList<>(unique.values());
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String routesHash(List<UserAccessServerRouteEntry> routes) throws Exception {
|
||||
List<String> logins = new ArrayList<>();
|
||||
for (UserAccessServerRouteEntry route : routes) {
|
||||
String login = normalize(route.getServerLogin());
|
||||
if (login != null) logins.add(login);
|
||||
}
|
||||
logins.sort(String::compareTo);
|
||||
private static String routesHash(UserAccessServerRouteEntry route) throws Exception {
|
||||
String normalized = route == null ? null : normalize(route.getServerLogin());
|
||||
String login = normalized == null ? "" : normalized;
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] hash = digest.digest(String.join("\n", logins).getBytes(StandardCharsets.UTF_8));
|
||||
byte[] hash = digest.digest(login.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder out = new StringBuilder(hash.length * 2);
|
||||
for (byte b : hash) out.append(String.format("%02x", b & 0xff));
|
||||
return out.toString();
|
||||
@@ -280,8 +142,6 @@ public final class DmDeliveryCoordinator {
|
||||
return createdAtMs + ATTEMPT_OFFSETS_MS[nextAttemptIndex];
|
||||
}
|
||||
|
||||
private record RouteAttempt(String serverLogin, String error) {}
|
||||
|
||||
private static String ownServerLogin() {
|
||||
return normalize(AppConfig.getInstance().getParam(SERVER_LOGIN_CONFIG));
|
||||
}
|
||||
@@ -296,16 +156,4 @@ public final class DmDeliveryCoordinator {
|
||||
String text = String.valueOf(e == null ? "unknown" : e.getMessage());
|
||||
return text.length() <= 500 ? text : text.substring(0, 500);
|
||||
}
|
||||
|
||||
private static ThreadFactory daemonThreadFactory(String prefix) {
|
||||
return new ThreadFactory() {
|
||||
private int sequence;
|
||||
@Override
|
||||
public synchronized Thread newThread(Runnable r) {
|
||||
Thread thread = new Thread(r, prefix + "-" + (++sequence));
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+28
-73
@@ -8,63 +8,17 @@ import utils.config.AppConfig;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/** Доставка DM-tombstone на единственные access-серверы обеих сторон. */
|
||||
public final class DmFederationService {
|
||||
private static final Logger log = LoggerFactory.getLogger(DmFederationService.class);
|
||||
private static final String CONFIG_KEY = "server.SHiNE.login";
|
||||
private static final RemoteDmSyncClient REMOTE = new RemoteDmSyncClient();
|
||||
private static final RemoteDmDeliveryClient REMOTE = new RemoteDmDeliveryClient();
|
||||
|
||||
private DmFederationService() {}
|
||||
|
||||
public static void fanOutPair(String fromLogin, String toLogin, String incomingBlobB64, String outgoingBlobB64) {
|
||||
try {
|
||||
Map<String, UserAccessServerRouteEntry> senderRoutes =
|
||||
routesByLogin(UserAccessServersCurrentDAO.getInstance().listByUserLogin(fromLogin));
|
||||
Map<String, UserAccessServerRouteEntry> recipientRoutes =
|
||||
routesByLogin(UserAccessServersCurrentDAO.getInstance().listByUserLogin(toLogin));
|
||||
|
||||
String ownServerLogin = ownServerLogin();
|
||||
for (UserAccessServerRouteEntry route : senderRoutes.values()) {
|
||||
if (isOwnServer(route, ownServerLogin)) continue;
|
||||
REMOTE.sendMessagePair(
|
||||
route.getServerLogin(), route.getServerUrl(),
|
||||
incomingBlobB64, outgoingBlobB64, ownServerLogin);
|
||||
}
|
||||
for (UserAccessServerRouteEntry route : recipientRoutes.values()) {
|
||||
if (isOwnServer(route, ownServerLogin)) continue;
|
||||
if (senderRoutes.containsKey(normalize(route.getServerLogin()))) continue;
|
||||
REMOTE.receiveIncomingMessage(
|
||||
route.getServerLogin(), route.getServerUrl(), incomingBlobB64, ownServerLogin);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("DM federation pair fan-out failed: from={} to={}", fromLogin, toLogin, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void fanOutIncomingToRecipientAccessServers(
|
||||
String toLogin,
|
||||
String incomingBlobB64,
|
||||
String sourceServerLogin
|
||||
) {
|
||||
try {
|
||||
Map<String, UserAccessServerRouteEntry> recipientRoutes =
|
||||
routesByLogin(UserAccessServersCurrentDAO.getInstance().listByUserLogin(toLogin));
|
||||
String ownServerLogin = ownServerLogin();
|
||||
String normalizedSource = normalize(sourceServerLogin);
|
||||
for (UserAccessServerRouteEntry route : recipientRoutes.values()) {
|
||||
String routeLogin = normalize(route.getServerLogin());
|
||||
if (routeLogin == null) continue;
|
||||
if (ownServerLogin != null && ownServerLogin.equalsIgnoreCase(routeLogin)) continue;
|
||||
if (normalizedSource != null && normalizedSource.equalsIgnoreCase(routeLogin)) continue;
|
||||
REMOTE.receiveIncomingMessage(
|
||||
route.getServerLogin(), route.getServerUrl(), incomingBlobB64, ownServerLogin);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("DM federation incoming relay failed: to={}", toLogin, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void fanOutDeleteMessage(String fromLogin, String toLogin, int messageType, String blobB64) {
|
||||
fanOutSingleDelete(fromLogin, toLogin, messageType, blobB64, true);
|
||||
}
|
||||
@@ -73,12 +27,17 @@ public final class DmFederationService {
|
||||
fanOutSingleDelete(fromLogin, toLogin, messageType, blobB64, false);
|
||||
}
|
||||
|
||||
private static void fanOutSingleDelete(String fromLogin, String toLogin, int messageType, String blobB64, boolean oneMessageDelete) {
|
||||
private static void fanOutSingleDelete(
|
||||
String fromLogin,
|
||||
String toLogin,
|
||||
int messageType,
|
||||
String blobB64,
|
||||
boolean oneMessageDelete
|
||||
) {
|
||||
try {
|
||||
Map<String, UserAccessServerRouteEntry> routes = routesByLogin(
|
||||
UserAccessServersCurrentDAO.getInstance().listByUserLogin(fromLogin),
|
||||
UserAccessServersCurrentDAO.getInstance().listByUserLogin(toLogin)
|
||||
);
|
||||
Map<String, UserAccessServerRouteEntry> routes = new LinkedHashMap<>();
|
||||
putPrimaryRoute(routes, fromLogin);
|
||||
putPrimaryRoute(routes, toLogin);
|
||||
String ownServerLogin = ownServerLogin();
|
||||
for (UserAccessServerRouteEntry route : routes.values()) {
|
||||
if (isOwnServer(route, ownServerLogin)) continue;
|
||||
@@ -89,33 +48,29 @@ public final class DmFederationService {
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("DM federation delete fan-out failed: from={} to={} type={}",
|
||||
log.warn("DM delete delivery failed: from={} to={} type={}",
|
||||
fromLogin, toLogin, messageType, e);
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private static Map<String, UserAccessServerRouteEntry> routesByLogin(
|
||||
List<UserAccessServerRouteEntry>... routeLists
|
||||
) {
|
||||
Map<String, UserAccessServerRouteEntry> out = new LinkedHashMap<>();
|
||||
for (List<UserAccessServerRouteEntry> routeList : routeLists) {
|
||||
for (UserAccessServerRouteEntry route : routeList) {
|
||||
if (route == null) continue;
|
||||
String login = normalize(route.getServerLogin());
|
||||
String address = route.getServerUrl() == null ? "" : route.getServerUrl().trim();
|
||||
if (login == null || address.isBlank()) continue;
|
||||
out.putIfAbsent(login, route);
|
||||
}
|
||||
private static void putPrimaryRoute(Map<String, UserAccessServerRouteEntry> out, String ownerLogin)
|
||||
throws Exception {
|
||||
List<UserAccessServerRouteEntry> routes =
|
||||
UserAccessServersCurrentDAO.getInstance().listByUserLogin(ownerLogin);
|
||||
for (UserAccessServerRouteEntry route : routes) {
|
||||
if (route == null) continue;
|
||||
String login = normalize(route.getServerLogin());
|
||||
String address = route.getServerUrl() == null ? "" : route.getServerUrl().trim();
|
||||
if (login == null || address.isBlank()) continue;
|
||||
out.putIfAbsent(login, route);
|
||||
return;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static boolean isOwnServer(UserAccessServerRouteEntry route, String ownServerLogin) {
|
||||
return ownServerLogin != null
|
||||
&& route != null
|
||||
&& route.getServerLogin() != null
|
||||
&& ownServerLogin.equalsIgnoreCase(route.getServerLogin());
|
||||
&& ownServerLogin.equals(normalize(route.getServerLogin()));
|
||||
}
|
||||
|
||||
private static String ownServerLogin() {
|
||||
@@ -124,7 +79,7 @@ public final class DmFederationService {
|
||||
|
||||
private static String normalize(String value) {
|
||||
if (value == null) return null;
|
||||
String s = value.trim().toLowerCase();
|
||||
return s.isEmpty() ? null : s;
|
||||
String normalized = value.trim().toLowerCase(Locale.ROOT);
|
||||
return normalized.isEmpty() ? null : normalized;
|
||||
}
|
||||
}
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package server.sync;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/** Клиент межсерверной доставки DM на единственный access-сервер получателя. */
|
||||
public final class RemoteDmDeliveryClient {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
public void receiveIncomingMessage(
|
||||
String targetServerLogin,
|
||||
String serverAddressRaw,
|
||||
String incomingBlobB64,
|
||||
String sourceServerLogin
|
||||
) throws Exception {
|
||||
String incomingJson = MAPPER.writeValueAsString(incomingBlobB64);
|
||||
String sourceServerLoginJson = toOptionalJsonField("sourceServerLogin", sourceServerLogin);
|
||||
JsonNode response = send(targetServerLogin, serverAddressRaw, """
|
||||
{
|
||||
"op":"ReceiveIncomingMessage",
|
||||
"requestId":%s,
|
||||
"payload":{
|
||||
"incomingBlobB64":%s%s
|
||||
}
|
||||
}
|
||||
""".formatted("%s", incomingJson, sourceServerLoginJson));
|
||||
ensureOk("ReceiveIncomingMessage", response);
|
||||
}
|
||||
|
||||
public void deleteMessage(String targetServerLogin, String serverAddressRaw, String blobB64) throws Exception {
|
||||
String blobJson = MAPPER.writeValueAsString(blobB64);
|
||||
JsonNode response = send(targetServerLogin, serverAddressRaw, """
|
||||
{
|
||||
"op":"DeleteMessage",
|
||||
"requestId":%s,
|
||||
"payload":{"blobB64":%s}
|
||||
}
|
||||
""".formatted("%s", blobJson));
|
||||
ensureOk("DeleteMessage", response);
|
||||
}
|
||||
|
||||
public void deleteConversation(String targetServerLogin, String serverAddressRaw, String blobB64) throws Exception {
|
||||
String blobJson = MAPPER.writeValueAsString(blobB64);
|
||||
JsonNode response = send(targetServerLogin, serverAddressRaw, """
|
||||
{
|
||||
"op":"DeleteConversation",
|
||||
"requestId":%s,
|
||||
"payload":{"blobB64":%s}
|
||||
}
|
||||
""".formatted("%s", blobJson));
|
||||
ensureOk("DeleteConversation", response);
|
||||
}
|
||||
|
||||
private JsonNode send(String targetServerLogin, String serverAddressRaw, String jsonTemplate) throws Exception {
|
||||
return ServerConnectionPool.getInstance().request(
|
||||
targetServerLogin,
|
||||
serverAddressRaw,
|
||||
jsonTemplate,
|
||||
ServerConnectionPool.Priority.REALTIME);
|
||||
}
|
||||
|
||||
private String toOptionalJsonField(String fieldName, String value) throws Exception {
|
||||
if (value == null || value.isBlank()) return "";
|
||||
return ",\n \"" + fieldName + "\":" + MAPPER.writeValueAsString(value.trim());
|
||||
}
|
||||
|
||||
private void ensureOk(String op, JsonNode response) {
|
||||
int status = response.path("status").asInt(500);
|
||||
if (status >= 200 && status < 300) return;
|
||||
String code = response.path("code").asText("");
|
||||
if (code.isBlank()) code = response.path("error").asText("");
|
||||
throw new IllegalStateException(op + " failed: status=" + status + " code=" + code);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1091,7 +1091,7 @@ public final class PostgresStorageRepository
|
||||
ELSE u.access_servers_json::jsonb
|
||||
END
|
||||
) WITH ORDINALITY AS access_server(login_value, ord)
|
||||
WHERE ord <= 2
|
||||
WHERE ord = 1
|
||||
) AS access_server
|
||||
JOIN solana_user_pda_current s
|
||||
ON LOWER(s.login) = LOWER(btrim(access_server.login_value))
|
||||
@@ -1138,7 +1138,7 @@ public final class PostgresStorageRepository
|
||||
ELSE u.access_servers_json::jsonb
|
||||
END
|
||||
) WITH ORDINALITY AS access_server(login_value, ord)
|
||||
WHERE ord <= 2
|
||||
WHERE ord = 1
|
||||
) AS access_server
|
||||
WHERE LOWER(btrim(access_server.login_value)) = LOWER(p_server_login)
|
||||
LOOP
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package server.sync;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import shine.db.entities.DmDeliveryStateEntry;
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/** Пятисекундный воркер повторной доставки DM получателю. */
|
||||
public final class PeriodicDmDeliveryService {
|
||||
private static final Logger log = LoggerFactory.getLogger(PeriodicDmDeliveryService.class);
|
||||
private static final AtomicBoolean STARTED = new AtomicBoolean(false);
|
||||
private static final ScheduledExecutorService SCHEDULER =
|
||||
Executors.newSingleThreadScheduledExecutor(daemonThreadFactory("dm-worker-dispatcher"));
|
||||
private static final ThreadPoolExecutor DELIVERY_EXECUTOR = new ThreadPoolExecutor(
|
||||
4, 4, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(500),
|
||||
daemonThreadFactory("dm-delivery"), new ThreadPoolExecutor.DiscardPolicy());
|
||||
|
||||
private PeriodicDmDeliveryService() {}
|
||||
|
||||
public static void startOrLog() {
|
||||
if (!isEnabled()) {
|
||||
log.info("DM delivery worker disabled by dm.delivery.enabled=false");
|
||||
return;
|
||||
}
|
||||
if (!STARTED.compareAndSet(false, true)) return;
|
||||
long pollSeconds = configLong("dm.worker.pollSeconds", 5L, 1L, 60L);
|
||||
SCHEDULER.scheduleWithFixedDelay(
|
||||
PeriodicDmDeliveryService::tickSafe, 0L, pollSeconds, TimeUnit.SECONDS);
|
||||
log.info("DM delivery worker scheduled every {} seconds", pollSeconds);
|
||||
}
|
||||
|
||||
private static void tickSafe() {
|
||||
try {
|
||||
int limit = (int) configLong("dm.worker.dueLimit", 100L, 1L, 1000L);
|
||||
for (DmDeliveryStateEntry row : DmDeliveryCoordinator.listDue(limit)) {
|
||||
DELIVERY_EXECUTOR.execute(() -> DmDeliveryCoordinator.processDueEntry(row));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("DM delivery dispatcher failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isEnabled() {
|
||||
String raw = AppConfig.getInstance().getParam("dm.delivery.enabled");
|
||||
return raw == null || raw.isBlank() || Boolean.parseBoolean(raw.trim());
|
||||
}
|
||||
|
||||
private static long configLong(String key, long defaultValue, long min, long max) {
|
||||
String raw = AppConfig.getInstance().getParam(key);
|
||||
if (raw == null || raw.isBlank()) return defaultValue;
|
||||
try { return Math.max(min, Math.min(max, Long.parseLong(raw.trim()))); }
|
||||
catch (Exception ignored) { return defaultValue; }
|
||||
}
|
||||
|
||||
private static ThreadFactory daemonThreadFactory(String prefix) {
|
||||
return new ThreadFactory() {
|
||||
private int sequence;
|
||||
@Override public synchronized Thread newThread(Runnable r) {
|
||||
Thread t = new Thread(r, prefix + "-" + (++sequence));
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,256 +0,0 @@
|
||||
package server.sync;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.messages.DmSyncApplySupport;
|
||||
import shine.db.dao.UserAccessServersCurrentDAO;
|
||||
import shine.db.dao.UserSettingsDAO;
|
||||
import shine.db.dao.UserSettingsSyncPeerStateDAO;
|
||||
import shine.db.entities.UserAccessServerRouteEntry;
|
||||
import shine.db.entities.UserSettingEntry;
|
||||
import shine.db.entities.UserSettingsSyncPeerStateEntry;
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class PeriodicUserSettingsSyncService {
|
||||
private static final Logger log = LoggerFactory.getLogger(PeriodicUserSettingsSyncService.class);
|
||||
private static final String SERVER_LOGIN_CONFIG = "server.SHiNE.login";
|
||||
private static final AtomicBoolean STARTED = new AtomicBoolean(false);
|
||||
private static final RemoteUserSettingsSyncClient REMOTE = new RemoteUserSettingsSyncClient();
|
||||
private static final RemoteDmSyncClient DM_REMOTE = new RemoteDmSyncClient();
|
||||
private static final UserAccessServersCurrentDAO ACCESS_DAO = UserAccessServersCurrentDAO.getInstance();
|
||||
private static final UserSettingsDAO SETTINGS_DAO = UserSettingsDAO.getInstance();
|
||||
private static final UserSettingsSyncPeerStateDAO STATE_DAO = UserSettingsSyncPeerStateDAO.getInstance();
|
||||
|
||||
private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread t = new Thread(r, "periodic-user-settings-sync");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
}
|
||||
});
|
||||
|
||||
private PeriodicUserSettingsSyncService() {}
|
||||
|
||||
public static void startOrLog() {
|
||||
if (!isEnabled()) {
|
||||
log.info("Periodic user settings sync disabled by user.settings.sync.enabled=false");
|
||||
return;
|
||||
}
|
||||
if (!STARTED.compareAndSet(false, true)) return;
|
||||
long initialDelaySec = configLong("user.settings.sync.initialDelaySeconds", 90L, 0L, 3600L);
|
||||
long periodHours = configLong("user.settings.sync.periodHours", 6L, 1L, 168L);
|
||||
EXECUTOR.scheduleWithFixedDelay(
|
||||
PeriodicUserSettingsSyncService::runCycleSafe,
|
||||
initialDelaySec,
|
||||
TimeUnit.HOURS.toSeconds(periodHours),
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
EXECUTOR.scheduleWithFixedDelay(
|
||||
PeriodicUserSettingsSyncService::runRequestedCycleSafe,
|
||||
5L, 5L, TimeUnit.SECONDS);
|
||||
log.info("Periodic user settings sync scheduled: first run in {} seconds, then every {} hours", initialDelaySec, periodHours);
|
||||
}
|
||||
|
||||
private static void runCycleSafe() {
|
||||
try {
|
||||
runCycle();
|
||||
} catch (Exception e) {
|
||||
log.error("Periodic user settings sync failed unexpectedly", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void runRequestedCycleSafe() {
|
||||
if (DmSyncWakeSignal.consume()) runCycleSafe();
|
||||
}
|
||||
|
||||
private static void runCycle() throws Exception {
|
||||
String ownServerLogin = normalize(AppConfig.getInstance().getParam(SERVER_LOGIN_CONFIG));
|
||||
if (ownServerLogin == null) {
|
||||
log.warn("Periodic user settings sync skipped: {} is empty", SERVER_LOGIN_CONFIG);
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> ownersRaw = ACCESS_DAO.listUserLoginsByServerLogin(ownServerLogin);
|
||||
Set<String> owners = new LinkedHashSet<>(ownersRaw);
|
||||
if (owners.isEmpty()) {
|
||||
log.info("Periodic user settings sync skipped: no local access-server users for {}", ownServerLogin);
|
||||
return;
|
||||
}
|
||||
|
||||
int syncedPeers = 0;
|
||||
int appliedItems = 0;
|
||||
int pushedItems = 0;
|
||||
int appliedDmItems = 0;
|
||||
for (String ownerLogin : owners) {
|
||||
List<UserAccessServerRouteEntry> routes = ACCESS_DAO.listByUserLogin(ownerLogin);
|
||||
for (UserAccessServerRouteEntry route : routes) {
|
||||
if (route == null) continue;
|
||||
String remoteLogin = normalize(route.getServerLogin());
|
||||
String remoteUrl = route.getServerUrl();
|
||||
if (remoteLogin == null || remoteUrl == null || remoteUrl.isBlank()) continue;
|
||||
if (remoteLogin.equals(ownServerLogin)) continue;
|
||||
|
||||
try {
|
||||
SyncStats stats = syncOwnerWithRemote(ownerLogin, route);
|
||||
appliedItems += stats.applied();
|
||||
pushedItems += stats.pushed();
|
||||
appliedDmItems += stats.appliedDm();
|
||||
syncedPeers++;
|
||||
} catch (Exception e) {
|
||||
STATE_DAO.updateError(ownerLogin, route.getServerLogin(), remoteUrl, String.valueOf(e));
|
||||
log.warn("Periodic user settings sync peer failed: owner={} remoteServer={} reason={}",
|
||||
ownerLogin, route.getServerLogin(), String.valueOf(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Periodic access-data sync finished: owners={} peers={} settingsApplied={} settingsPushed={} dmApplied={}",
|
||||
owners.size(), syncedPeers, appliedItems, pushedItems, appliedDmItems);
|
||||
}
|
||||
|
||||
private static SyncStats syncOwnerWithRemote(String ownerLogin, UserAccessServerRouteEntry route) throws Exception {
|
||||
int limit = (int) configLong("user.settings.sync.batchLimit", 500L, 1L, 1000L);
|
||||
int maxBytes = (int) configLong("user.settings.sync.batchMaxBytes", 3_000_000L, 64_000L, 5_000_000L);
|
||||
int maxPages = (int) configLong("user.settings.sync.maxPagesPerPeer", 50L, 1L, 500L);
|
||||
|
||||
UserSettingsSyncPeerStateEntry state;
|
||||
try (Connection c = getDbConnection()) {
|
||||
state = STATE_DAO.getOrCreate(c, ownerLogin, route.getServerLogin(), route.getServerUrl());
|
||||
}
|
||||
long cursorTimeMs = state.getCursorTimeMs();
|
||||
String cursorSettingKey = state.getCursorSettingKey() == null ? "" : state.getCursorSettingKey();
|
||||
int applied = 0;
|
||||
int pushed = 0;
|
||||
boolean bootstrapCompleted = false;
|
||||
|
||||
int appliedDm;
|
||||
try (RemoteSyncSession session = new RemoteSyncSession(route.getServerLogin(), route.getServerUrl())) {
|
||||
for (int page = 0; page < maxPages; page++) {
|
||||
RemoteUserSettingsSyncClient.RemoteUserSettingsBatch batch = REMOTE.userSettingsSyncBatch(
|
||||
session,
|
||||
ownerLogin,
|
||||
cursorTimeMs,
|
||||
cursorSettingKey,
|
||||
limit,
|
||||
maxBytes
|
||||
);
|
||||
|
||||
try (Connection c = getDbConnection()) {
|
||||
for (RemoteUserSettingsSyncClient.RemoteUserSettingsItem item : batch.items()) {
|
||||
UserSettingEntry entry = new UserSettingEntry(
|
||||
item.login(),
|
||||
item.settingType(),
|
||||
item.settingKey(),
|
||||
item.timeMs(),
|
||||
item.valueText(),
|
||||
item.valueNum(),
|
||||
item.clientKey(),
|
||||
item.signature(),
|
||||
true
|
||||
);
|
||||
int changed = SETTINGS_DAO.upsertIfNewer(c, entry);
|
||||
if (changed > 0) applied++;
|
||||
}
|
||||
}
|
||||
|
||||
cursorTimeMs = Math.max(cursorTimeMs, batch.nextTimeMs());
|
||||
cursorSettingKey = batch.nextSettingKey() == null ? "" : batch.nextSettingKey();
|
||||
bootstrapCompleted = !batch.hasMore();
|
||||
STATE_DAO.updateSuccess(ownerLogin, route.getServerLogin(), route.getServerUrl(), cursorTimeMs, cursorSettingKey, bootstrapCompleted);
|
||||
|
||||
if (!batch.hasMore() || batch.items().isEmpty()) break;
|
||||
}
|
||||
|
||||
try (Connection c = getDbConnection()) {
|
||||
List<UserSettingEntry> unsynced = SETTINGS_DAO.listUnsyncedByLogin(c, ownerLogin, limit);
|
||||
for (UserSettingEntry entry : unsynced) {
|
||||
REMOTE.upsertUserSetting(session, entry, true);
|
||||
SETTINGS_DAO.markSynced(c, entry.getLogin(), entry.getSettingType(), entry.getSettingKey());
|
||||
pushed++;
|
||||
}
|
||||
}
|
||||
|
||||
int dmLimit = (int) configLong("dm.sync.batchLimit", 200L, 1L, 500L);
|
||||
int dmMaxBytes = (int) configLong("dm.sync.batchMaxBytes", 3_000_000L, 64_000L, 5_000_000L);
|
||||
int dmMaxPages = (int) configLong("dm.sync.maxPagesPerPeer", 20L, 1L, 500L);
|
||||
appliedDm = syncDmInSameSession(session, ownerLogin, dmLimit, dmMaxBytes, dmMaxPages);
|
||||
}
|
||||
|
||||
if (!bootstrapCompleted) {
|
||||
log.info("Periodic user settings sync peer paused by page limit: owner={} remoteServer={} maxPages={}",
|
||||
ownerLogin, route.getServerLogin(), maxPages);
|
||||
}
|
||||
return new SyncStats(applied, pushed, appliedDm);
|
||||
}
|
||||
|
||||
private static int syncDmInSameSession(
|
||||
RemoteSyncSession session, String ownerLogin, int limit, int maxBytes, int maxPages
|
||||
) throws Exception {
|
||||
long cursorMs = 0L;
|
||||
String cursorKey = "";
|
||||
List<String> acknowledgements = new ArrayList<>();
|
||||
int applied = 0;
|
||||
for (int page = 0; page < maxPages; page++) {
|
||||
RemoteDmSyncClient.RemoteDmBatch batch = DM_REMOTE.dmSyncBatch(
|
||||
session, ownerLogin, cursorMs, cursorKey, Math.min(limit, 500), maxBytes, acknowledgements);
|
||||
acknowledgements = new ArrayList<>();
|
||||
for (RemoteDmSyncClient.RemoteDmItem item : batch.items()) {
|
||||
if (item == null || item.syncId() == null || item.syncId().isBlank()) continue;
|
||||
DmSyncApplySupport.applySyncedItem(ownerLogin, item.syncId(), item.blobsB64());
|
||||
acknowledgements.add(item.syncId());
|
||||
applied++;
|
||||
}
|
||||
cursorMs = batch.nextStoredAtMs();
|
||||
cursorKey = batch.nextMessageKey() == null ? "" : batch.nextMessageKey();
|
||||
if (!batch.hasMore() || batch.items().isEmpty()) {
|
||||
if (!acknowledgements.isEmpty()) {
|
||||
DM_REMOTE.dmSyncBatch(session, ownerLogin, 0L, "",
|
||||
Math.min(limit, 500), maxBytes, acknowledgements);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
|
||||
private static java.sql.Connection getDbConnection() throws Exception {
|
||||
return shine.db.DbController.getInstance().getConnection();
|
||||
}
|
||||
|
||||
private static boolean isEnabled() {
|
||||
String raw = AppConfig.getInstance().getParam("user.settings.sync.enabled");
|
||||
return raw == null || raw.isBlank() || Boolean.parseBoolean(raw.trim());
|
||||
}
|
||||
|
||||
private static long configLong(String key, long defaultValue, long min, long max) {
|
||||
String raw = AppConfig.getInstance().getParam(key);
|
||||
if (raw == null || raw.isBlank()) return defaultValue;
|
||||
try {
|
||||
long parsed = Long.parseLong(raw.trim());
|
||||
return Math.max(min, Math.min(max, parsed));
|
||||
} catch (Exception ignored) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static String normalize(String value) {
|
||||
if (value == null) return null;
|
||||
String s = value.trim().toLowerCase(Locale.ROOT);
|
||||
return s.isEmpty() ? null : s;
|
||||
}
|
||||
|
||||
private record SyncStats(int applied, int pushed, int appliedDm) {}
|
||||
}
|
||||
@@ -8,8 +8,7 @@ import org.slf4j.LoggerFactory;
|
||||
import server.debug.DebugApiConfigurator;
|
||||
import server.sync.BlockchainResyncRecoveryOnStartup;
|
||||
import server.sync.PeriodicBlockchainSyncService;
|
||||
import server.sync.PeriodicDmSyncService;
|
||||
import server.sync.PeriodicUserSettingsSyncService;
|
||||
import server.sync.PeriodicDmDeliveryService;
|
||||
import server.sync.SolanaUsersSyncStartupService;
|
||||
import server.sync.SyncServersBootstrapService;
|
||||
import server.sync.ServerConnectionPool;
|
||||
@@ -109,8 +108,7 @@ public final class WsServer {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(
|
||||
() -> ServerConnectionPool.getInstance().close(),
|
||||
"server-connection-pool-shutdown"));
|
||||
PeriodicDmSyncService.startOrLog();
|
||||
PeriodicUserSettingsSyncService.startOrLog();
|
||||
PeriodicDmDeliveryService.startOrLog();
|
||||
server.join();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,13 +34,10 @@ sync.importUserProfileFromPartner.enabled=false
|
||||
# ------------------------------------------------------------
|
||||
server.version=${projectVersion}
|
||||
|
||||
# Доставка и межсерверная синхронизация личных сообщений.
|
||||
dm.sync.enabled=true
|
||||
# Доставка личных сообщений на единственный access-сервер получателя.
|
||||
dm.delivery.enabled=true
|
||||
dm.worker.pollSeconds=5
|
||||
dm.worker.dueLimit=100
|
||||
dm.sync.batchLimit=200
|
||||
dm.sync.batchMaxBytes=3000000
|
||||
dm.sync.maxPagesPerPeer=20
|
||||
server.pool.pingIdleSeconds=120
|
||||
server.pool.pongTimeoutSeconds=15
|
||||
server.pool.requestTimeoutSeconds=12
|
||||
|
||||
Reference in New Issue
Block a user