Merge "Configure ulimits for gerrit file descriptors"
diff --git a/Configuration.md b/Configuration.md
index 0e98ad7..2985e5b 100644
--- a/Configuration.md
+++ b/Configuration.md
@@ -43,6 +43,10 @@
 * `JGIT_CACHE_SIZE`: Maximum number of bytes to load and cache in memory from pack files.
   See [Gerrit documentation](https://gerrit-review.googlesource.com/Documentation/config-gerrit.html#core.packedGitLimit)
   for more details. `3g` by default.
+* `GERRIT_CONTAINER_FDS_SOFT_LIMIT`: The soft limit for file descriptors allowed in the Gerrit container.
+`1024` by default.
+* `GERRIT_CONTAINER_FDS_HARD_LIMIT`: The hard limit for file descriptors allowed in the Gerrit container
+`1024` by default.
 * `INTERNET_GATEWAY_ID`: Optional. Id of the existing Internet Gateway.
   If not set, create a new Internet Gateway
 * `VPC_ID`: Optional. Id of the existing VPC.
diff --git a/Makefile.common b/Makefile.common
index 645d35d..8f52152 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -67,4 +67,12 @@
 set-ldap-account-pattern:
 ifdef LDAP_ACCOUNT_PATTERN
 		$(eval LDAP_ACCOUNT_PATTERN_PARAM := ParameterKey=LDAPAccountPattern,ParameterValue=\"$(LDAP_ACCOUNT_PATTERN)\")
+endif
+
+set-optional-gerrit-ulimits:
+ifdef GERRIT_CONTAINER_FDS_SOFT_LIMIT
+		$(eval GERRIT_ULIMITS := ParameterKey=FileDescriptorsSoftLimit,ParameterValue=$(GERRIT_CONTAINER_FDS_SOFT_LIMIT))
+endif
+ifdef GERRIT_CONTAINER_FDS_HARD_LIMIT
+		$(eval GERRIT_ULIMITS := $(GERRIT_ULIMITS) ParameterKey=FileDescriptorsHardLimit,ParameterValue=$(GERRIT_CONTAINER_FDS_HARD_LIMIT))
 endif
\ No newline at end of file
diff --git a/dual-master/Makefile b/dual-master/Makefile
index f481ba3..c00cc72 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -86,7 +86,7 @@
 		ParameterKey=SubnetIdProp,ParameterValue=$(SUBNET_ID) \
 		$(CLUSTER_OPTIONAL_PARAMS)
 
-service-master-1: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern
+service-master-1: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits
 ifdef GERRIT_MASTER1_INSTANCE_ID
 		$(eval MASTER1_SERVICE_OPTIONAL_PARAMS := $(MASTER1_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_MASTER1_INSTANCE_ID))
 endif
@@ -136,9 +136,10 @@
 		$(MASTER1_SERVICE_OPTIONAL_PARAMS) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
-		$(SMTP_OPTIONAL_PARAMS)
+		$(SMTP_OPTIONAL_PARAMS) \
+		$(GERRIT_ULIMITS)
 
-service-master-2: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern
+service-master-2: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits
 ifdef GERRIT_MASTER2_INSTANCE_ID
 		$(eval MASTER2_SERVICE_OPTIONAL_PARAMS := $(MASTER2_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_MASTER2_INSTANCE_ID))
 endif
@@ -189,7 +190,8 @@
 		$(MASTER2_SERVICE_OPTIONAL_PARAMS) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
-		$(SMTP_OPTIONAL_PARAMS)
+		$(SMTP_OPTIONAL_PARAMS) \
+		$(GERRIT_ULIMITS)
 
 service-replication:
 	$(AWS_FC_COMMAND) create-stack \
@@ -207,7 +209,7 @@
 		ParameterKey=HostedZoneName,ParameterValue=$(HOSTED_ZONE_NAME) \
 		ParameterKey=GitReplicationSubdomain,ParameterValue=$(GIT_REPLICATION_SUBDOMAIN)
 
-service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern
+service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern set-optional-gerrit-ulimits
 ifdef GERRIT_SLAVE_INSTANCE_ID
 		$(eval SLAVE_SERVICE_OPTIONAL_PARAMS := $(SLAVE_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_SLAVE_INSTANCE_ID))
 endif
@@ -239,7 +241,8 @@
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(SLAVE_SERVICE_OPTIONAL_PARAMS) \
-		$(METRICS_CW_OPTIONAL_PARAMS)
+		$(METRICS_CW_OPTIONAL_PARAMS) \
+		$(GERRIT_ULIMITS)
 
 service-lb:
 ifdef LOAD_BALANCER_SCHEME
diff --git a/dual-master/cf-service-master.yml b/dual-master/cf-service-master.yml
index 4566b62..c2c8d85 100644
--- a/dual-master/cf-service-master.yml
+++ b/dual-master/cf-service-master.yml
@@ -74,6 +74,14 @@
       Description: Gerrit Heap limit
       Type: String
       Default: 6g
+  FileDescriptorsSoftLimit:
+    Description: The soft limit for file descriptors allowed in the Gerrit container
+    Type: Number
+    Default: 1024
+  FileDescriptorsHardLimit:
+    Description: The hard limit for file descriptors allowed in the Gerrit container
+    Type: Number
+    Default: 1024
   JgitCacheSize:
       Description: JGit cache size
       Type: String
@@ -320,7 +328,10 @@
                       Value: !Ref MultiSiteZookeeperConnectString
                     - Name: MULTISITE_ZOOKEEPER_ROOT_NODE
                       Value: !Ref MultiSiteZookeeperRootNode
-
+                  Ulimits:
+                    - Name: nofile
+                      HardLimit: !Ref FileDescriptorsHardLimit
+                      SoftLimit: !Ref FileDescriptorsSoftLimit
                   MountPoints:
                     - SourceVolume: !FindInMap ['Gerrit', 'Volume', 'Git']
                       ContainerPath: /var/gerrit/git
diff --git a/dual-master/cf-service-slave.yml b/dual-master/cf-service-slave.yml
index 4f27e14..5c5acb3 100644
--- a/dual-master/cf-service-slave.yml
+++ b/dual-master/cf-service-slave.yml
@@ -122,6 +122,14 @@
       Description: Gerrit Heap limit
       Type: String
       Default: 6g
+  FileDescriptorsSoftLimit:
+      Description: The soft limit for file descriptors allowed in the Gerrit container
+      Type: Number
+      Default: 1024
+  FileDescriptorsHardLimit:
+      Description: The hard limit for file descriptors allowed in the Gerrit container
+      Type: Number
+      Default: 1024
   JgitCacheSize:
       Description: JGit cache size
       Type: String
@@ -261,6 +269,10 @@
                       Value: !Ref MetricsCloudwatchDryRun
                     - Name: METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST
                       Value: !Join [',', !Ref MetricsCloudwatchExcludeMetrics]
+                  Ulimits:
+                    - Name: nofile
+                      HardLimit: !Ref FileDescriptorsHardLimit
+                      SoftLimit: !Ref FileDescriptorsSoftLimit
                   MountPoints:
                     - SourceVolume: !Ref GerritGitVolume
                       ContainerPath: /var/gerrit/git
diff --git a/dual-master/setup.env.template b/dual-master/setup.env.template
index 57e46aa..d9e6377 100644
--- a/dual-master/setup.env.template
+++ b/dual-master/setup.env.template
@@ -59,3 +59,6 @@
 HA_PROXY_MAX_COUNT:=2
 HA_PROXY_DESIRED_COUNT:=2
 MASTER_MAX_COUNT:=2
+
+GERRIT_CONTAINER_FDS_SOFT_LIMIT:=32768
+GERRIT_CONTAINER_FDS_HARD_LIMIT:=32768
\ No newline at end of file
diff --git a/master-slave/Makefile b/master-slave/Makefile
index 6c1c67e..2dfa92e 100644
--- a/master-slave/Makefile
+++ b/master-slave/Makefile
@@ -53,7 +53,7 @@
 		ParameterKey=SubnetIdProp,ParameterValue=$(SUBNET_ID) \
 		$(CLUSTER_OPTIONAL_PARAMS)
 
-service-master: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern
+service-master: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits
 ifdef LOAD_BALANCER_SCHEME
 		$(eval MASTER_SERVICE_OPTIONAL_PARAMS := $(MASTER_SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -91,9 +91,11 @@
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(MASTER_SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
-		$(SMTP_OPTIONAL_PARAMS)
+		$(SMTP_OPTIONAL_PARAMS) \
+		$(GERRIT_ULIMITS)
 
-service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern
+
+service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern set-optional-gerrit-ulimits
 ifdef LOAD_BALANCER_SCHEME
 		$(eval SLAVE_SERVICE_OPTIONAL_PARAMS := $(SLAVE_SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -125,7 +127,8 @@
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(SLAVE_SERVICE_OPTIONAL_PARAMS) \
-		$(METRICS_CW_OPTIONAL_PARAMS)
+		$(METRICS_CW_OPTIONAL_PARAMS) \
+		$(GERRIT_ULIMITS)
 
 dns-routing:
 	$(AWS_FC_COMMAND) create-stack \
diff --git a/master-slave/cf-service-master.yml b/master-slave/cf-service-master.yml
index e4060f2..86aa525 100644
--- a/master-slave/cf-service-master.yml
+++ b/master-slave/cf-service-master.yml
@@ -110,6 +110,14 @@
       Description: Gerrit Heap limit
       Type: String
       Default: 6g
+  FileDescriptorsSoftLimit:
+      Description: The soft limit for file descriptors allowed in the Gerrit container
+      Type: Number
+      Default: 1024
+  FileDescriptorsHardLimit:
+      Description: The hard limit for file descriptors allowed in the Gerrit container
+      Type: Number
+      Default: 1024
   JgitCacheSize:
       Description: JGit cache size
       Type: String
@@ -283,6 +291,10 @@
                       Value: !Ref MetricsCloudwatchDryRun
                     - Name: METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST
                       Value: !Join [',', !Ref MetricsCloudwatchExcludeMetrics]
+                  Ulimits:
+                    - Name: nofile
+                      HardLimit: !Ref FileDescriptorsHardLimit
+                      SoftLimit: !Ref FileDescriptorsSoftLimit
                   MountPoints:
                     - SourceVolume: !Ref GerritGitVolume
                       ContainerPath: /var/gerrit/git
diff --git a/master-slave/cf-service-slave.yml b/master-slave/cf-service-slave.yml
index 6449b5a..0540fe9 100644
--- a/master-slave/cf-service-slave.yml
+++ b/master-slave/cf-service-slave.yml
@@ -122,6 +122,14 @@
       Description: Gerrit Heap limit
       Type: String
       Default: 6g
+  FileDescriptorsSoftLimit:
+    Description: The soft limit for file descriptors allowed in the Gerrit container
+    Type: Number
+    Default: 1024
+  FileDescriptorsHardLimit:
+    Description: The hard limit for file descriptors allowed in the Gerrit container
+    Type: Number
+    Default: 1024
   JgitCacheSize:
       Description: JGit cache size
       Type: String
@@ -258,6 +266,10 @@
                       Value: !Ref MetricsCloudwatchDryRun
                     - Name: METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST
                       Value: !Join [',', !Ref MetricsCloudwatchExcludeMetrics]
+                  Ulimits:
+                    - Name: nofile
+                      HardLimit: !Ref FileDescriptorsHardLimit
+                      SoftLimit: !Ref FileDescriptorsSoftLimit
                   MountPoints:
                     - SourceVolume: !Ref GerritGitVolume
                       ContainerPath: /var/gerrit/git
diff --git a/master-slave/setup.env.template b/master-slave/setup.env.template
index 98389ae..0c519c4 100644
--- a/master-slave/setup.env.template
+++ b/master-slave/setup.env.template
@@ -39,3 +39,6 @@
 METRICS_CLOUDWATCH_JVM_ENABLED:=true
 METRICS_CLOUDWATCH_DRY_RUN:=false
 METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST:=foo.*,bar.*
+
+GERRIT_CONTAINER_FDS_SOFT_LIMIT:=32768
+GERRIT_CONTAINER_FDS_HARD_LIMIT:=32768
diff --git a/single-master/Makefile b/single-master/Makefile
index 13d0205..9076898 100644
--- a/single-master/Makefile
+++ b/single-master/Makefile
@@ -52,7 +52,7 @@
 		ParameterKey=SubnetIdProp,ParameterValue=$(SUBNET_ID) \
 		$(CLUSTER_OPTIONAL_PARAMS)
 
-service: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern
+service: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits
 ifdef LOAD_BALANCER_SCHEME
 		$(eval SERVICE_OPTIONAL_PARAMS := $(SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -88,7 +88,8 @@
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
-		$(SMTP_OPTIONAL_PARAMS)
+		$(SMTP_OPTIONAL_PARAMS) \
+		$(GERRIT_ULIMITS)
 
 dns-routing:
 	$(AWS_FC_COMMAND) create-stack \
diff --git a/single-master/cf-service.yml b/single-master/cf-service.yml
index 7302e6a..cc6f12a 100644
--- a/single-master/cf-service.yml
+++ b/single-master/cf-service.yml
@@ -88,6 +88,14 @@
       Description: vCPU to allocate to the Gerrit container
       Type: Number
       Default: 1024
+  FileDescriptorsSoftLimit:
+    Description: The soft limit for file descriptors allowed in the Gerrit container
+    Type: Number
+    Default: 1024
+  FileDescriptorsHardLimit:
+    Description: The hard limit for file descriptors allowed in the Gerrit container
+    Type: Number
+    Default: 1024
   GerritHeapLimit:
       Description: Gerrit Heap limit
       Type: String
@@ -270,6 +278,10 @@
                       ContainerPath: /var/gerrit/db
                     - SourceVolume: !Ref GerritLogsVolume
                       ContainerPath: /var/gerrit/logs
+                  Ulimits:
+                    - Name: nofile
+                      HardLimit: !Ref FileDescriptorsHardLimit
+                      SoftLimit: !Ref FileDescriptorsSoftLimit
                   Cpu: !Ref GerritCPU
                   Memory: !Ref GerritRAM
                   PortMappings:
diff --git a/single-master/setup.env.template b/single-master/setup.env.template
index dc9adc5..a9609d0 100644
--- a/single-master/setup.env.template
+++ b/single-master/setup.env.template
@@ -27,4 +27,7 @@
 METRICS_CLOUDWATCH_INITIAL_DELAY:=0s
 METRICS_CLOUDWATCH_JVM_ENABLED:=true
 METRICS_CLOUDWATCH_DRY_RUN:=false
-METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST:=foo.*,bar.*
\ No newline at end of file
+METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST:=foo.*,bar.*
+
+GERRIT_CONTAINER_FDS_SOFT_LIMIT:=32768
+GERRIT_CONTAINER_FDS_HARD_LIMIT:=32768
\ No newline at end of file