16 Ağustos 2022 Salı

Business Exceptions

Giriş
Şöyle bir kural var deniliyor
Try not to create new custom exceptions if they do not have useful information for client code.
Ancak ilave bilgi içermiyor diye sadece düz Exception fırlatmak bence doğru değil. Sebebi şöyle. Çünkü  düz exception fırlatırsak bu durumda JVM'den veya bizim kodumuzdan gelen exceptionları ayırt edemiyoruz.  Bu yüzden boş ta olsa kendi Business Exception ata sınıfımızı tanımlamakta fayda var
Do not throw just an Exception! It means you have to catch Exception, which in turn means you also catch all RuntimeExceptions, therefore all NPEs etc!
Peki Neden Ayırt Etmek Lazım?
Örnek
Bir seferinden şöyle bir şey gerekmişti. Kod hem veri tabanına hem de farklı bir yere veri yazıyordu. Eğer her hangi bir hata varsa işlemi iptal etmek gerekiyordu. Bir tane AbortException tanımlandı. Bu sınıf şöyledi. İlave bir üye alan veya bilgi içermemesine rağmen, işlemin iptal etmesini sağlıyordu.
public class AbortException extends Exception {}
Dolayısıyla illa ilave bilgi içermersine yani şöyle olmasına gerek yok
public class DuplicateUsernameException extends Exception {
  public DuplicateUsernameException (String username){....}
  public String requestedUsername(){...}
  public String[] availableNames(){...}
}

Kütüphaneler
Bir çok kütüphane, plugin kendi exception hiyerarşisini sunuyor ve geliştiricilerin bu hiyerarşiden kalıtmasını istiyor. 
Örnek
Bir keresinde şöyle bir kod vardı
public class ServiceException extends Frwxception {
  ...
}


10 Ağustos 2022 Çarşamba

SOI-2 - Development Review - Geliştirme Bitmeden Önce Yapılır

Giriş
Açıklaması şöyle. Yani geliştirme başladıktan sonra ancak bitmeden önce yapılması iyi olur
How to approach SOI#2 in your DO-178C project...


The Development milestone (SOI#2)

Before you have finished your Development, but when you have examples of each of your Development artifacts (typically 60%-80% of your total expected artifacts), you should conduct SOI#2 with your certification authority.

SOI#2 focuses on the development process and artifacts, but the review also considers verification processes that should be running concurrently with Development – specifically, the review activities being implemented.

SOI#2 may also look forward to the verification phase to see if there are any examples where verification activities have provided feedback to development activities.

This may include, for example, test case development or test environment development providing feedback to your requirements and design processes to ensure that test activities can completely verify the functionality expressed in your requirements and design.
DER gereksinim dokümanlarını, bu dokümanların testini, test sonuçlarını, gereksinimi karşılayan kodları inceler ve sorgular. Örneğin bunlar arasındaki izlenebilirliğe bakabilir.

Kullanılan araçların versiyon listesine bakabilir. Testler için robustness var mı diye sorgulayabilir.  Bu testler varsa hangi gereksinimlere izlenebilirliği olduğuna bakılır. Eğer çok ciddi bir problem yoksa ufak tefek sıkıntılar "finding" (bulgu) olarak kaydedilir ve bir sonraki aşamada düzeltilmesini isteyebilir.

2 Ağustos 2022 Salı

CLH (Craig, Landin, and Hagersten) lock

Giriş
Bir çeşit spin lock. Açıklaması şöyle. java.util.concurrent.locks.AbstractQueuedSynchronizer.Node buna benzer bir mantık kullanıyor
The CLH lock is a scalable, high-performance, and fair spin lock based on a linked list. The application thread only spins on local variables. It always reads the state of the pre-node. If it finds that the pre-node releases the lock, it ends from spin
CLH Lock'a gelmeden Önce

1. Test And Set  Lock
Şöyle yaparız
public class TASLock implements Lock {
AtomicBoolean state = new AtomicBoolean(false); public void lock() { while (state.getAndSet()) {} } public void unlock() { state.set(false); } }
2. Test&Test&Set Lock
Şöyle yaparız
public class TTASLock implements Lock {
  AtomicBoolean state = new AtomicBoolean(false);
  public void lock() {
    while (true) {
      while(state.get()) {}
      if(!state.getAndSet())
        return;
     }
 }
 public void unlock() {
   state.set(false);
 }
}
Kuyruk Kullanan Kilitler
Bu kilitler FIFO fairness, fast lock release, low contention gibi iyi özellikler sunuyorlar, ancak abort işlemini iyi desteklemiyorlar

3. Anderson queue lock (ALock)
Açıklaması şöyle
ALock: Acquiring the Lock

• To acquire the lock, each thread atomically increments the tail field
• If the flag is true, the lock is acquired
• Otherwise, spin until the flag is true

ALock: Contention

• If another thread wants to acquire the lock, it applies get&increment
• The thread spins because the flag is false

ALock: Releasing the Lock

• The first thread releases the lock by setting the next slot to true
• The second thread notices the change and gets the lock

Örnek
Şöyle yaparız. Burada atomic olan sadece AtomicInteger yani benim kuyruktaki yerimi gösteren sayaç. 
public class Alock implements Lock {
//One flag per thread boolean[] flags = {true,false,...,false}; AtomicInteger next = new AtomicInteger(0); //Thread-local variable ThreadLocal<Integer> mySlot; public void lock() { //Take the next slot mySlot = next.getAndIncrement(); while (!flags[mySlot % n]) {} flags[mySlot % n] = false; } public void unlock() { //Tell next thread to go flags[(mySlot+1) % n] = true; } }
CLH Lock Gerçekleştirimi
Açıklaması şöyle. Burada Anderson kilidinden farklı olarak kilitlemek için benden önceki düğümün bayrağı üzerinde bekliyorum. 
The threads could update own flag and spin on their predecessor’s flag
This is basically what the CLH lock does, but using a linked list instead of an array
Örnek
Şöyle yaparız
static class CLHLock implements Lock {
    AtomicReference<QNode> tail;
    ThreadLocal<QNode> myNode, myPred;

  public CLHLock() {
    tail = new AtomicReference<QNode>(new QNode());

    this.myNode = new ThreadLocal<QNode>() {
      protected QNode initialValue() {
        return new QNode();
      }
    };

    this.myPred = new ThreadLocal<QNode>() {
      protected QNode initialValue() {
        return null;
      }
    };
  }

  public void lock() {
    QNode qnode = this.myNode.get();
    qnode.locked.set(true);  

    QNode pred = this.tail.getAndSet(qnode); //Add myself to tail. Tail is AtomicRef.
    myPred.set(pred); //Save my predecessor
    while (pred.locked.get()) {} //Wait if my predecessor is locked
  }

  public void unlock() {
    QNode qnode = this.myNode.get(); 
    qnode.locked.set(false); //Tell next thread to go      
    this.myNode.set(this.myPred.get()); 
  }

  static class QNode {  
    public AtomicBoolean locked = new AtomicBoolean(false);
  }
}

29 Temmuz 2022 Cuma

aws komutu

Giriş
Aynı şeyi "AWS Management Console" kullanarak yapmak daha kolay. 

"https://foo.awsapps.com/start" adresine gidilir ve "Management Console" linkine tıklanır

Kurulum

Windows
Örnek
Şöyle yaparız
C:\> msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
configure seçeneği
aws configure seçeneği yazısına taşıdım

dynamodb seçeneği
Örnek
Şöyle yaparız
$ aws dynamodb scan    \
--table-name Devices   \
--endpoint-url http://localhost:4566
ec2 seçeneği
Örnek -describe-vpcs
Şöyle yaparız
$ aws ec2 describe-vpcs
Örnek - describe-instances
Şöyle yaparız
aws ec2 describe-instances --region us-east-1
ecr seçeneği - Container Publish Etmek İçindir
aws ecr seçeneği yazısına taşıdım

secretmanager seçeneği
Örnek
Şöyle yaparız
aaws secretsmanager create-secret 
  --name /secret/db-credential 
  --secret-string '{"dbuser": "user1", "dbpassword": "password"}'
İsmi /secret/db-credential olan ve içinde iki tane secret olan bir şey elde ederiz. Şeklen şöyle


s3 seçeneği
aws s3 seçeneği yazısına taşıdım

sqs seçeneği
Örnek - create-queue
Şöyle yaparız
aws --endpoint-url=http://127.0.0.1:4576 sqs create-queue --queue-name test-queue
Örnek - list-queues
Şöyle yaparız
aws --endpoint-url=http://127.0.0.1:4566 sqs list-queues

--version seçeneği
Örnek
Şöyle yaparız
C:\>aws --version
aws-cli/2.8.0 Python/3.9.11 Windows/10 exe/AMD64 prompt/off
Örnek
Şöyle yaparız
$ aws --version 
aws-cli/2.1.29 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0



Amazon Web Service (AWS) - Elastic Kubernetes services(EKS)

Giriş
Açıklaması şöyle
Amazon EKS is a managed service that makes it easy for you to run Kubernetes on AWS without needing to install and operate your own Kubernetes control plane or worker nodes.
Açıklaması şöyle
Amazon EKS helps developers create, deploy and scale Kubernetes applications on-premises or in the AWS cloud. EKS automates tasks such as patching, updates and node provisioning, thereby helping organizations to ship reliable, secure and highly scalable clusters. While doing so, EKS takes away all the tedium and manual configuration tasks to manage Kubernetes clusters, helping to cut-down on efforts of performing repetitive tasks to run your applications.

Since EKS is an upstream offering of Kubernetes, you can use all existing Kubernetes plugins and tools for your application. This service automatically deploys Kubernetes with three master nodes across multiple availability zones for ultimate reliability and resilience. With Role Based Access Control (RBAC) and Amazon’s Identity and Access Management (IAM) entities, you can easily manage security in your AWS clusters using Kubernetes tools, such as kubectl. As one of its core features, EKS allows launching and managing Kubernetes clusters easy using a few easy steps.
AWS EKS vs AWS ECS
Açıklaması şöyle. Yani AWS EKS kullanmak daha mantıklı. 
We chose Kubernetes (EKS) over ECS for several reasons, but the main one was due to its ability to scale up and scale down faster, making it a very effective method. Based on the amount of documentation and actual development being done on the underlying systems; Kubernetes is much more recent and updated than ECS. In addition, it allows developers to have a far less complex infrastructure setup, along with much more complete tooling.

Kubernetes makes it easier to see and understand everything that’s going on with all your deployments. In ECS we had several different services running in different places, but there was no way to see an overview of everything that was running and what was being deployed.

In addition, Kubernetes has several third-party extensions you can add to further improve your experience. For example, our dev team is using Keel which watches for new versions of our services to be pushed and then deploys them automatically.

Another reason to use Kubernetes is because it’s a system made up of several standards that can “run anywhere”. This means if we ever had to do on-prem again it would be easy to port our infrastructure to an on-prem Kubernetes cluster because it speaks the same language. It’s also possible to run a Kubernetes cluster on your own laptop if you want to mirror what was deployed onto a local machine.

Finally, Kubernetes is a more industry-standard system than ECS that exists on every cloud provider, not just AWS. Plus, it’s easier these days to find developers who understand Kubernetes compared to ECS.
Ayrıca How To Migrate From ECS to EKS and the #1 Trick To Make EKS Easier yazısına da bakılabilir. Bu yazıda ECS proprietary (tescilli) teknoloji olduğu için uygulamayı başka bir bulut sağlayıcısına port etmek gerekirse zorluklar olabileceğinden bahsediliyor.

Şeklen şöyle

Bileşenler şöyle
An Amazon EKS cluster consists of the following core objects
- EKS control plane
- EKS nodes(Worker Nodes) that are registered with the control plane
- AWS Fargate Profiles
- VPC
Ekran görüntüsü şöyle. "Add cluster" düğmesi ile yeni cluster eklenir

Cluster'daki node'ların ekran görüntüsü şöyle

Cluster Büyüklüğü
EKS Design: Choosing the cluster and node size yazısına bakılabilir. Yazıdaki bir cümle şöyle
... you will need to decide on the initial cluster and node sizes, and then keep adjusting them until you reach the correct utilization level to achieve a balance between cost and reliability. You can target a utilisation level of between 70 and 80% unless you have a solid justification for using a different level.

eksctl komutu
EKS ile çalışmayı kolaylaştırır. eksctl komutu yazısına taşıdım

Amazon Web Service (AWS) App Runner

Giriş
Açıklaması şöyle
App Runner provides a layer of abstraction of top of Fargate, which lets you just pick an image to run and it does all the autoscaling, load balancing, and SSL configuration for you. 

Another really cool feature is that you can actually connect it to a GitHub project and it will automatically build & deploy when it detects changes (watch out for those build fees though!!). ...

The other option for using App Runner is to upload a container image, which will have everything that’s needed already built, packaged, and ready to go. 
VPC Connector ile container veri tabanına bağlanabilir.

Internet of Things - IoT Duyarga (Sensor) ve Actuator

Giriş
Sensor Türkçeye Duyarga, Actuator ise Eyleyici olarak çevriliyor

Duyargalar şeklen şöyle
Eyleyiciler şeklen şöyle