Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
K
key_value-server
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
amir.yosef
key_value-server
Commits
aa94992a
Commit
aa94992a
authored
Aug 25, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimizing the nature of each class
parent
94618a3c
Changes
19
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
26 additions
and
25 deletions
+26
-25
Client.java
src/client/Client.java
+2
-2
MasterClient.java
src/client/master/MasterClient.java
+2
-2
ReplicaClient.java
src/client/replica/ReplicaClient.java
+2
-2
CommandInvoker.java
src/command/CommandInvoker.java
+1
-1
EchoCommand.java
src/command/EchoCommand.java
+1
-1
FullRsyncCommand.java
src/command/FullRsyncCommand.java
+1
-1
GetCommand.java
src/command/GetCommand.java
+1
-1
InfoCommand.java
src/command/InfoCommand.java
+1
-1
PingCommand.java
src/command/PingCommand.java
+1
-1
ReplConfCommand.java
src/command/ReplConfCommand.java
+1
-1
SetCommand.java
src/command/SetCommand.java
+1
-1
UnknownCommand.java
src/command/UnknownCommand.java
+1
-1
ReplicaSetCommand.java
src/command/replica/ReplicaSetCommand.java
+1
-1
ClientCommandHandler.java
src/handlers/ClientCommandHandler.java
+1
-1
ConnectionHandler.java
src/handlers/replica/ConnectionHandler.java
+1
-1
ReplicaCommandHandler.java
src/handlers/replica/ReplicaCommandHandler.java
+1
-1
ReplicaConnectionService.java
src/server/ReplicaConnectionService.java
+4
-3
SendToReplica.java
src/server/SendToReplica.java
+2
-2
Server.java
src/server/Server.java
+1
-1
No files found.
src/client/Client.java
View file @
aa94992a
...
@@ -10,7 +10,7 @@ import java.net.Socket;
...
@@ -10,7 +10,7 @@ import java.net.Socket;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
abstract
class
Client
extends
Thread
{
public
abstract
class
Client
{
protected
final
CommandParser
commandParser
;
protected
final
CommandParser
commandParser
;
protected
final
Socket
socket
;
protected
final
Socket
socket
;
protected
BufferedReader
reader
;
protected
BufferedReader
reader
;
...
@@ -27,7 +27,7 @@ public abstract class Client extends Thread {
...
@@ -27,7 +27,7 @@ public abstract class Client extends Thread {
this
.
socket
=
socket
;
this
.
socket
=
socket
;
}
}
@Override
public
void
run
()
{
public
void
run
()
{
try
(
OutputStream
outputStream
=
socket
.
getOutputStream
())
{
try
(
OutputStream
outputStream
=
socket
.
getOutputStream
())
{
if
(
reader
==
null
)
{
if
(
reader
==
null
)
{
...
...
src/client/master/MasterClient.java
View file @
aa94992a
...
@@ -12,7 +12,7 @@ import java.util.List;
...
@@ -12,7 +12,7 @@ import java.util.List;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
MasterClient
extends
Client
{
public
final
class
MasterClient
extends
Client
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
MasterClient
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
MasterClient
.
class
.
getName
());
public
MasterClient
(
Socket
socket
)
{
public
MasterClient
(
Socket
socket
)
{
...
@@ -31,7 +31,7 @@ public class MasterClient extends Client {
...
@@ -31,7 +31,7 @@ public class MasterClient extends Client {
commandHandler
.
execute
();
commandHandler
.
execute
();
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
logger
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
logger
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
}
}
}
}
}
}
src/client/replica/ReplicaClient.java
View file @
aa94992a
...
@@ -15,7 +15,7 @@ import java.util.concurrent.Executors;
...
@@ -15,7 +15,7 @@ import java.util.concurrent.Executors;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
ReplicaClient
extends
Client
{
public
final
class
ReplicaClient
extends
Client
{
private
final
CommandByteCounter
commandByteCounter
;
private
final
CommandByteCounter
commandByteCounter
;
private
final
ExecutorService
executorService
;
private
final
ExecutorService
executorService
;
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaClient
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaClient
.
class
.
getName
());
...
@@ -38,7 +38,7 @@ public class ReplicaClient extends Client {
...
@@ -38,7 +38,7 @@ public class ReplicaClient extends Client {
executorService
.
submit
(
command
);
executorService
.
submit
(
command
);
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
logger
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
logger
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
}
}
}
}
...
...
src/command/CommandInvoker.java
View file @
aa94992a
...
@@ -4,7 +4,7 @@ import java.io.IOException;
...
@@ -4,7 +4,7 @@ import java.io.IOException;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
CommandInvoker
{
public
final
class
CommandInvoker
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
CommandInvoker
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
CommandInvoker
.
class
.
getName
());
public
static
byte
[]
invoke
(
CommandExecutable
<
byte
[]>
command
)
throws
IOException
{
public
static
byte
[]
invoke
(
CommandExecutable
<
byte
[]>
command
)
throws
IOException
{
...
...
src/command/EchoCommand.java
View file @
aa94992a
...
@@ -4,7 +4,7 @@ import util.Response;
...
@@ -4,7 +4,7 @@ import util.Response;
import
java.util.List
;
import
java.util.List
;
public
class
EchoCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
EchoCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
...
...
src/command/FullRsyncCommand.java
View file @
aa94992a
...
@@ -10,7 +10,7 @@ import java.io.ByteArrayOutputStream;
...
@@ -10,7 +10,7 @@ import java.io.ByteArrayOutputStream;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
public
class
FullRsyncCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
FullRsyncCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
SendToReplica
replicaSender
;
private
final
SendToReplica
replicaSender
;
private
final
RdbFileInfo
rdbFileInfo
;
private
final
RdbFileInfo
rdbFileInfo
;
private
final
OutputStream
outputStream
;
private
final
OutputStream
outputStream
;
...
...
src/command/GetCommand.java
View file @
aa94992a
...
@@ -6,7 +6,7 @@ import util.Response;
...
@@ -6,7 +6,7 @@ import util.Response;
import
java.util.List
;
import
java.util.List
;
public
class
GetCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
GetCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
...
...
src/command/InfoCommand.java
View file @
aa94992a
...
@@ -7,7 +7,7 @@ import java.util.List;
...
@@ -7,7 +7,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
public
class
InfoCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
InfoCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
ServerInfo
configuration
=
ServerInfo
.
getInstance
();
private
final
ServerInfo
configuration
=
ServerInfo
.
getInstance
();
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
List
<
String
>
args
;
List
<
String
>
args
;
...
...
src/command/PingCommand.java
View file @
aa94992a
...
@@ -3,7 +3,7 @@ package command;
...
@@ -3,7 +3,7 @@ package command;
import
model.Command
;
import
model.Command
;
public
class
PingCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
PingCommand
implements
CommandExecutable
<
byte
[]>
{
@Override
@Override
public
byte
[]
execute
()
{
public
byte
[]
execute
()
{
...
...
src/command/ReplConfCommand.java
View file @
aa94992a
...
@@ -4,7 +4,7 @@ import model.Command;
...
@@ -4,7 +4,7 @@ import model.Command;
import
java.util.List
;
import
java.util.List
;
public
class
ReplConfCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
ReplConfCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
...
...
src/command/SetCommand.java
View file @
aa94992a
...
@@ -5,7 +5,7 @@ import util.Response;
...
@@ -5,7 +5,7 @@ import util.Response;
import
java.util.List
;
import
java.util.List
;
public
class
SetCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
SetCommand
implements
CommandExecutable
<
byte
[]>
{
private
static
final
byte
[]
OK_RESPONSE
=
"+OK\r\n"
.
getBytes
();
private
static
final
byte
[]
OK_RESPONSE
=
"+OK\r\n"
.
getBytes
();
private
static
final
byte
[]
ERROR_RESPONSE
=
Response
.
getResponse
(
"wrong args"
);
private
static
final
byte
[]
ERROR_RESPONSE
=
Response
.
getResponse
(
"wrong args"
);
...
...
src/command/UnknownCommand.java
View file @
aa94992a
...
@@ -3,7 +3,7 @@ package command;
...
@@ -3,7 +3,7 @@ package command;
import
java.util.Objects
;
import
java.util.Objects
;
public
class
UnknownCommand
implements
CommandExecutable
<
byte
[]>
{
public
final
class
UnknownCommand
implements
CommandExecutable
<
byte
[]>
{
private
static
String
message
;
private
static
String
message
;
public
UnknownCommand
(
String
message
)
{
public
UnknownCommand
(
String
message
)
{
...
...
src/command/replica/ReplicaSetCommand.java
View file @
aa94992a
...
@@ -7,7 +7,7 @@ import java.util.List;
...
@@ -7,7 +7,7 @@ import java.util.List;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
ReplicaSetCommand
implements
CommandExecutable
<
Void
>
{
public
final
class
ReplicaSetCommand
implements
CommandExecutable
<
Void
>
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaSetCommand
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaSetCommand
.
class
.
getName
());
...
...
src/handlers/ClientCommandHandler.java
View file @
aa94992a
...
@@ -16,7 +16,7 @@ import java.util.List;
...
@@ -16,7 +16,7 @@ import java.util.List;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
ClientCommandHandler
{
public
final
class
ClientCommandHandler
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ClientCommandHandler
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ClientCommandHandler
.
class
.
getName
());
private
final
List
<
String
>
commands
;
private
final
List
<
String
>
commands
;
private
final
OutputStream
os
;
private
final
OutputStream
os
;
...
...
src/handlers/replica/ConnectionHandler.java
View file @
aa94992a
...
@@ -15,7 +15,7 @@ import java.net.Socket;
...
@@ -15,7 +15,7 @@ import java.net.Socket;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
ConnectionHandler
{
public
final
class
ConnectionHandler
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
());
private
final
Socket
socket
;
private
final
Socket
socket
;
...
...
src/handlers/replica/ReplicaCommandHandler.java
View file @
aa94992a
...
@@ -12,7 +12,7 @@ import java.util.List;
...
@@ -12,7 +12,7 @@ import java.util.List;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
ReplicaCommandHandler
implements
CommandProcessable
{
public
final
class
ReplicaCommandHandler
implements
CommandProcessable
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaCommandHandler
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaCommandHandler
.
class
.
getName
());
private
final
List
<
String
>
commands
;
private
final
List
<
String
>
commands
;
private
final
ReplicaCommandFactory
factory
=
ReplicaCommandFactory
.
getInstance
();
private
final
ReplicaCommandFactory
factory
=
ReplicaCommandFactory
.
getInstance
();
...
...
src/server/ReplicaConnectionService.java
View file @
aa94992a
...
@@ -13,7 +13,7 @@ import java.util.concurrent.Executors;
...
@@ -13,7 +13,7 @@ import java.util.concurrent.Executors;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
ReplicaConnectionServic
e
{
public
final
class
ReplicaConnectionService
implements
AutoCloseabl
e
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaConnectionService
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ReplicaConnectionService
.
class
.
getName
());
private
final
CommandParser
commandParser
;
private
final
CommandParser
commandParser
;
private
final
int
port
;
private
final
int
port
;
...
@@ -23,7 +23,7 @@ public class ReplicaConnectionService {
...
@@ -23,7 +23,7 @@ public class ReplicaConnectionService {
public
ReplicaConnectionService
(
String
[]
masterPortAndHost
,
int
port
)
throws
IOException
{
public
ReplicaConnectionService
(
String
[]
masterPortAndHost
,
int
port
)
throws
IOException
{
this
.
port
=
port
;
this
.
port
=
port
;
this
.
commandParser
=
new
CommandParser
();
this
.
commandParser
=
new
CommandParser
();
this
.
executorService
=
Executors
.
new
CachedThreadPool
();
this
.
executorService
=
Executors
.
new
SingleThreadExecutor
();
if
(
masterPortAndHost
.
length
>
0
)
{
if
(
masterPortAndHost
.
length
>
0
)
{
this
.
socket
=
new
Socket
(
masterPortAndHost
[
0
],
Integer
.
parseInt
(
masterPortAndHost
[
1
]));
this
.
socket
=
new
Socket
(
masterPortAndHost
[
0
],
Integer
.
parseInt
(
masterPortAndHost
[
1
]));
...
@@ -41,7 +41,7 @@ public class ReplicaConnectionService {
...
@@ -41,7 +41,7 @@ public class ReplicaConnectionService {
try
{
try
{
logger
.
info
(
"Ending handshake"
);
logger
.
info
(
"Ending handshake"
);
Client
client
=
new
ReplicaClient
(
bufferedReader
,
socket
);
Client
client
=
new
ReplicaClient
(
bufferedReader
,
socket
);
executorService
.
submit
(
client
);
executorService
.
submit
(
client
:
:
run
);
logger
.
info
(
"ReplicaConnectionService: CONNECTION SUCCESS"
);
logger
.
info
(
"ReplicaConnectionService: CONNECTION SUCCESS"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
...
@@ -57,6 +57,7 @@ public class ReplicaConnectionService {
...
@@ -57,6 +57,7 @@ public class ReplicaConnectionService {
}
}
}
}
@Override
public
void
close
()
{
public
void
close
()
{
executorService
.
shutdown
();
executorService
.
shutdown
();
if
(
socket
!=
null
&&
!
socket
.
isClosed
())
{
if
(
socket
!=
null
&&
!
socket
.
isClosed
())
{
...
...
src/server/SendToReplica.java
View file @
aa94992a
...
@@ -24,7 +24,7 @@ public class SendToReplica implements Closeable {
...
@@ -24,7 +24,7 @@ public class SendToReplica implements Closeable {
private
SendToReplica
()
{
private
SendToReplica
()
{
this
.
connectedReplicas
=
new
ConcurrentLinkedQueue
<>();
this
.
connectedReplicas
=
new
ConcurrentLinkedQueue
<>();
this
.
commands
=
new
ConcurrentLinkedQueue
<>();
this
.
commands
=
new
ConcurrentLinkedQueue
<>();
this
.
executorService
=
Executors
.
new
SingleThread
Executor
();
this
.
executorService
=
Executors
.
new
VirtualThreadPerTask
Executor
();
this
.
countCommands
=
new
AtomicInteger
(
0
);
this
.
countCommands
=
new
AtomicInteger
(
0
);
}
}
...
@@ -50,9 +50,9 @@ public class SendToReplica implements Closeable {
...
@@ -50,9 +50,9 @@ public class SendToReplica implements Closeable {
OutputStream
outputStream
=
replica
.
os
();
OutputStream
outputStream
=
replica
.
os
();
outputStream
.
write
(
commandBytes
);
outputStream
.
write
(
commandBytes
);
outputStream
.
flush
();
outputStream
.
flush
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
logger
.
log
(
Level
.
SEVERE
,
"Failed to send command to replica "
,
e
);
logger
.
log
(
Level
.
SEVERE
,
"Failed to send command to replica "
,
e
);
connectedReplicas
.
remove
(
replica
);
}
}
});
});
}
}
...
...
src/server/Server.java
View file @
aa94992a
...
@@ -12,7 +12,7 @@ import java.util.concurrent.Executors;
...
@@ -12,7 +12,7 @@ import java.util.concurrent.Executors;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
public
class
Server
implements
AutoCloseable
{
public
final
class
Server
implements
AutoCloseable
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
Server
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
Server
.
class
.
getName
());
private
final
int
PORT
;
private
final
int
PORT
;
private
final
String
role
;
private
final
String
role
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment